using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var map = NArr(n - 1); var tree = new List<(int to, int len)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int len)>(); foreach (var edge in map) { tree[edge[0] - 1].Add((edge[1] - 1, edge[2])); tree[edge[1] - 1].Add((edge[0] - 1, edge[2])); } var count = new int[n]; WriteLine(DFS(n, 0, -1, tree, count)); } static long DFS(int n, int cur, int prev, List<(int to, int len)>[] tree, int[] count) { var ans = 0L; ++count[cur]; foreach (var next in tree[cur]) { if (next.to == prev) continue; ans += DFS(n, next.to, cur, tree, count); ans += (long)next.len * count[next.to] * (n - count[next.to]) * 2; count[cur] += count[next.to]; } return ans; } }