#include using namespace std; typedef long long ll; typedef pair pii; typedef pair pll; #define LLINF 9223372036854775807 #define MOD ll(1e9+7) #define all(x) (x).begin(),(x).end() #define dbg(x) cerr<<#x<<": "<> graph; vector visited(200005,-1); ll ans = 0; ll dfs(ll now, ll n, ll w){ visited[now] = 1; ll ret = 1; for(int i = 0; i < graph[now].size(); i++){ if(visited[graph[now][i].first] == -1){ ret += dfs(graph[now][i].first, n, graph[now][i].second); } } ans += 2*ret*(n-ret)*w; return ret; } int main(){ ll n; cin >> n; graph.resize(n); for(int i = 0; i < n-1; i++){ ll a, b, c; cin >> a >> b >> c; a--; b--; graph[a].push_back({b,c}); graph[b].push_back({a,c}); } dfs(0,n,0); cout << ans << endl; return 0; }