#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)<; using vi = vector; using vll = vector; vector G[200005]; ll ans; int N; int dfs(int u, int p) { int res = 1; each(e, G[u]) { if (e.first != p) { int num = dfs(e.first, u); ans += (ll)num * (N - num) * e.second * 2; res += num; } } return res; } void solve() { cin >> N; rep(i, N - 1) { int u, v, w; cin >> u >> v >> w; --u; --v; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } dfs(0, -1); cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }