func main() class Node() +var children: list +var size: int *func ctor() do me.children :: #list end func +func update(parent: Node) do me.children.head() for i(0, ^me.children - 1) var child: Node :: me.children.get() if(child <>& parent) do child.update(me) do me.size :+ child.size end if do me.children.next() end for do me.size :+ 1 end func end class var n: int :: cui@inputInt() var nodes: []Node :: #[n]Node for i(0, n - 1) do nodes[i] :: #Node end for var u: []int :: #[n - 1]int var v: []int :: #[n - 1]int var w: []int :: #[n - 1]int for i(0, n - 2) do u[i] :: cui@inputInt() - 1 do v[i] :: cui@inputInt() - 1 do w[i] :: cui@inputInt() do nodes[u[i]].children.add(nodes[v[i]]) do nodes[v[i]].children.add(nodes[u[i]]) end for do nodes[0].update(null) var ans: int :: 0 for i(0, n - 2) var size1: int :: [nodes[u[i]].size, nodes[v[i]].size].min() var size2: int :: n - size1 do ans :+ size1 * size2 * w[i] end for do ans :* 2 do cui@print("\{ans}\n") end func