local n = io.read("*n") local node = {} for i = 1, n do node[i] = {} node[i].edge, node[i].dup, node[i].asked, node[i].edgenum = {}, 1, false, 0 end for i = 1, n - 1 do local u, v, w = io.read("*n", "*n", "*n") node[u].edge[v] = w node[u].edgenum = node[u].edgenum + 1 node[v].edge[u] = w node[v].edgenum = node[v].edgenum + 1 end local tasks = {} local tasknum = 0 local done = 0 for i = 1, n do if node[i].edgenum == 1 then table.insert(tasks, i) tasknum = tasknum + 1 end end local ret = 0 while done < tasknum do done = done + 1 local idx = tasks[done] node[idx].asked = true for dst, len in pairs(node[idx].edge) do if not node[dst].asked then local d = node[idx].dup ret = ret + d * (n - d) * len node[dst].edgenum = node[dst].edgenum - 1 node[dst].dup = node[dst].dup + node[idx].dup if node[dst].edgenum == 1 then table.insert(tasks, dst) tasknum = tasknum + 1 end break end end end print(ret * 2)