#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace std; int n; long long ans = 0; vector>>p; vectors; void df(int cur, int prev, long long d) { for (int i = 0; i < p[cur].size(); i++) { int next = p[cur][i].first; if (next != prev)df(next, cur, p[cur][i].second); } if(prev == -1) return; s[prev] += s[cur]; ans += d*s[cur] * (n - s[cur]) * 2; } int main() { cin >> n; p.resize(n); s.resize(n, 1); for (int i = 0; i < n-1; i++) { int a, b, d; cin >> a >> b >> d; a--; b--; p[a].push_back({b,d}); p[b].push_back({a,d }); } df(0, -1, -1); cout << ans << endl; return 0; }