#include using namespace std; #define ll long long #define rep(i, n) for (int i = 0; i < (n); i++) #define P pair #define LP pair template void chmax(T& a, T b) { a = max(a, b); }; template void chmin(T& a, T b) { a = min(a, b); }; struct Edge { int to, c; Edge(int to, int c) : to(to), c(c) {} }; int n; vector G[200005]; int dp[200005]; void dfs(int v, int p=-1) { dp[v] = 1; for (auto e : G[v]) { if (e.to == p) continue; dfs(e.to, v); dp[v] += dp[e.to]; } } ll ans = 0; void dfs2(int v, int p=-1) { for (auto e : G[v]) { if (e.to == p) continue; ans += (ll)e.c*dp[e.to]*(n-dp[e.to]); dfs2(e.to, v); } } int main() { cin >> n; rep(i,n-1) { int u, v, w; cin >> u >> v >> w; u--, v--; G[u].push_back(Edge(v,w)); G[v].push_back(Edge(u,w)); } dfs(0); dfs2(0); ans *= 2; cout << ans << endl; return 0; }