func main() class Node() +var parent: Node +var children: list +var num: int *func ctor() do me.children :: #list end func +func calcNum() do me.children.head() for i(0, ^me.children - 1) var child: Node :: me.children.get() if(child.parent =& null) do child.parent :: me do child.calcNum() do me.num :+ child.num end if do me.children.next() end for do me.num :+ 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].parent :: nodes[0] do nodes[0].calcNum() var ans: int :: 0 for i(0, n - 2) var num1: int if(nodes[u[i]].parent =& nodes[v[i]]) do num1 :: nodes[u[i]].num else do num1 :: nodes[v[i]].num end if var num2: int :: n - num1 do ans :+ num1 * num2 * w[i] end for do ans :* 2 do cui@print("\{ans}\n") end func