// yukicoder: 872 All Tree Path // 2019.9.6 bal4u #include #include #include typedef long long ll; //// 高速入力 #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } int N; int hi[200005], *to[200005], *w[200005]; ll ans; int dfs(int now, int par) { int i, j, s, f; s = 1; for (i = 0; i < hi[now]; i++) if ((j = to[now][i]) != par) { s += (f = dfs(j, now)); ans += ((ll)w[now][i] * f * (N-f)) << 1; } return s; } int main() { int i, k, u, v; int *memo, sz; N = in(); memo = malloc(N*sizeof(int)*3); sz = 0; for (i = 1; i < N; i++) { u = in()-1, v = in()-1; memo[sz++] = u, memo[sz++] = v, memo[sz++] = in(); hi[u]++, hi[v]++; } for (i = 0; i < N; i++) if (hi[i]) { to[i] = malloc(hi[i] * sizeof(int)); w[i] = malloc(hi[i] * sizeof(int)); } memset(hi, 0, sizeof(int)*N); i = 0; while (i < sz) { u = memo[i++], v = memo[i++]; k = hi[u]++, to[u][k] = v, w[u][k] = memo[i]; k = hi[v]++, to[v][k] = u, w[v][k] = memo[i++]; } dfs(0, -1); printf("%lld\n", ans); return 0; }