#include using namespace std; typedef long long ll; #define F first #define S second #define pii pair #define eb emplace_back #define all(v) v.begin(), v.end() #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep3(i, l, n) for (int i = l; i < (n); ++i) #define sz(v) (int)v.size() #define inf (int)(1e9+7) #define abs(x) (x >= 0 ? x : -(x)) #define ceil(a, b) a / b + !!(a % b) ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; } template T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } int dist[200001]; vector > v; ll ans; int n; void dfs(int cur, int prev, int d) { for (auto nxt : v[cur]) { if (nxt.F == prev) continue; dfs(nxt.F, cur, nxt.S); } if (prev == -1) return; dist[prev] += dist[cur]; // 根ではいらない操作 ans += 1LL * d * dist[cur] * (n - dist[cur]) * 2; // 根ではdist[cur] = 0 } int main() { cin >> n; v.resize(n); rep(i, n) dist[i] = 1; rep(i, n - 1) { int u, vv, w; cin >> u >> vv >> w; u--; vv--; v[u].eb(make_pair(vv, w)); v[vv].eb(make_pair(u, w)); } dfs(0, -1, 0); cout << ans << endl; }