import strutils,sequtils,deques type edge = tuple[R,L,w : int] var N = stdin.readline.parseInt que = initDeque[int](1024) ln,rn : int used : array[200010, bool] cnt : array[200010, int] cnte : array[200010, int] edges = newSeq[edge](N) v,u,w : int nodes = newSeqWith(N + 1, newSeq[int](0)) ans : int64 for n in 0..(N - 2): (v,u,w) = stdin.readline.split.map(parseInt) v -= 1 u -= 1 edges[n] = (v, u, w) cnte[v] += 1 cnte[u] += 1 nodes[v].add(n) nodes[u].add(n) for n in 0.. 0: var e1 = que.popFirst if used[e1]: continue var e = edges[e1] used[e1] = true if cnte[e.R] == 1: ans += cnt[e.R].int64 * (N - cnt[e.R]).int64 * e.w.int64 cnt[e.L] += cnt[e.R] else: ans += cnt[e.L].int64 * (N - cnt[e.L]).int64 * e.w.int64 cnt[e.R] += cnt[e.L] cnte[e.R] -= 1 cnte[e.L] -= 1 if cnte[e.R] == 1: for t in nodes[e.R]: if used[t] == false: que.addLast(t) elif cnte[e.L] == 1: for t in nodes[e.L]: if used[t] == false: que.addLast(t) echo ans * 2