import strutils, sequtils, deques, math let read = iterator: string {.closure.} = for s in stdin.readAll.split: yield s proc main() = let n = read().parseInt type E = tuple[to, cost: int] var g = newSeqWith(n, newSeq[E]()) for i in 1.. 0: let (i, p, a, b) = q.popFirst var tot = 0 for e in g[i]: if e.to == p: tot += b + a * e.cost else: tot += dp[e.to] + size[e.to] * e.cost ans[i] = tot for e in g[i]: if e.to != p: let na = a + size[i] - size[e.to] nb = tot - dp[e.to] - size[e.to] * e.cost q.addLast((e.to, i, na, nb)) echo ans.sum main()