#include #define rep(i,n) for (int i = 0; i < (n); i ++) using namespace std; typedef long long ll; typedef pair PL; typedef pair P; const int INF = 1e9; const ll MOD = 1e9 + 7; const vector dy = {-1,0,1,0}; const vector dx = {0,1,0,-1}; const int MAXN = 200005; vector> G(MAXN); ll ans = 0; int dfs(int i,int n,int p = -1) { int ret = 1; for (P pa:G[i]) { int e = pa.first; int c = pa.second; if (e == p) continue; int tmp = dfs(e,n,i); ans += 2 * tmp * (n - tmp) * c; ret += tmp; } return ret; } int main() { int n; cin >> n; rep(_,n - 1) { int a,b,c; cin >> a >> b >> c; a --; b--; G[a].push_back(P(b,c)); G[b].push_back(P(a,c)); } dfs(0,n); cout << ans << endl; return 0; }