#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; ll dfs(ll i,ll n,ll p = -1) { ll ret = 1; for (PL pa:G[i]) { ll e = pa.first; ll c = pa.second; if (e == p) continue; ll tmp = dfs(e,n,i); ans += 2 * tmp * (n - tmp) * c; ret += tmp; } return ret; } int main() { ll n; cin >> n; rep(_,n - 1) { int a,b,c; cin >> a >> b >> c; a --; b--; G[a].push_back(PL(b,c)); G[b].push_back(PL(a,c)); } dfs(0,n); cout << ans << endl; return 0; }