#include using namespace std; using ll = long long; #define ALL(obj) (obj).begin(),(obj).end() #define SPEED cin.tie(0);ios::sync_with_stdio(false); template class Tree { Operator Op; using typeDist = decltype(Op.unitDist); size_t num; size_t ord; public: vector>> edge; vector depth; vector order; vector dist; vector> parent; vector>> child; vector,Operator::bit>> ancestor; vector size; vector> descendant; Tree(const int num):num(num),edge(num),depth(num,-1),order(num),dist(num){} //O(1) anytime void makeEdge(const int& from, const int& to, const typeDist w = 1) { edge[from].push_back({to,w}); } //O(N) anytime void makeDepth(const int root) { depth[root] = 0; dist[root] = Op.unitDist; ord = 0; dfs1(root); order[ord++] = root; } //O(N) anytime void makeDepth(void) { ord = 0; for(size_t root = 0; root < num; ++root) { if(depth[root] != -1) continue; depth[root] = 0; dist[root] = Op.unitDist; dfs1(root); order[ord++] = root; } } //for makeDepth void dfs1(int curr, int prev = -1){ for(auto& e:edge[curr]){ int next = e.first; if(next==prev) continue; depth[next] = depth[curr] + 1; dist[next] = Op.funcDist(dist[curr],e.second); dfs1(next,curr); order[ord++] = next; } } //O(N) after makeDepth void makeParent(void) { parent.resize(num,make_pair(num,Op.unitDist)); for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] > depth[e.first]) parent[i] = e; } //O(N) after makeDepth void makeChild(void) { child.resize(num); for (size_t i = 0; i < num; ++i) for (auto& e : edge[i]) if (depth[i] < depth[e.first]) child[i].push_back(e); } //O(NlogN) after makeDepth and makeParent void makeAncestor(void) { ancestor.resize(num); for (size_t i = 0; i < num; ++i) ancestor[i][0] = (parent[i].first!=num?parent[i]:make_pair(i,Op.unitLca)); for (size_t j = 1; j < Operator::bit; ++j) { for (size_t i = 0; i < num; ++i) { size_t k = ancestor[i][j - 1].first; ancestor[i][j] = Op.funcLca(ancestor[k][j - 1],ancestor[i][j - 1]); } } } //O(logN) after makeAncestor //return {lca,lca_dist} l and r must be connected pair lca(size_t l, size_t r) { if (depth[l] < depth[r]) swap(l, r); int diff = depth[l] - depth[r]; auto ancl = make_pair(l,Op.unitLca); auto ancr = make_pair(r,Op.unitLca); for (int j = 0; j < Operator::bit; ++j) { if (diff & (1 << j)) { ancl = Op.funcLca(ancestor[ancl.first][j],ancl); } } if(ancl.first==ancr.first) return ancl; for (int j = Operator::bit - 1; 0 <= j; --j) { if(ancestor[ancl.first][j].first!=ancestor[ancr.first][j].first) { ancl = Op.funcLca(ancestor[ancl.first][j],ancl); ancr = Op.funcLca(ancestor[ancr.first][j],ancr); } } ancl = Op.funcLca(ancestor[ancl.first][0],ancl); ancr = Op.funcLca(ancestor[ancr.first][0],ancr); return Op.funcLca(ancl,ancr); } //O(N) anytime int diameter(void){ makeDepth(0); int tmp = max_element(depth.begin(), depth.end()) - depth.begin(); makeDepth(tmp); return *max_element(depth.begin(), depth.end()); } //O(N^2) after makeDepth (include self) void makeDescendant(void) { descendant.resize(num); for (size_t i = 0; i < num; ++i) descendant[i].push_back(i); for (size_t i = 0; i < num; ++i) for (auto& e : edge[order[i]]) if (depth[order[i]] < depth[e.first]) for(auto k: descendant[e.first]) descendant[order[i]].push_back(k); } //O(N) after makeChild void makeSize(void) { size.resize(num,1); for (size_t i:order) for (auto e : child[i]) size[i] += size[e.first]; } }; template struct treeOperator{ static const size_t bit = 20; typeDist unitDist = 0; typeDist unitLca = 0; typeDist funcDist(const typeDist& parent,const typeDist& w){return parent+w;} pair funcLca(const pair& l,const pair& r){return make_pair(l.first,max(l.second,r.second));} }; //depth,dist //https://atcoder.jp/contests/abc126/tasks/abc126_d //child //https://atcoder.jp/contests/abc133/tasks/abc133_e //lca //https://atcoder.jp/contests/abc014/tasks/abc014_4 //weighted lca //https://atcoder.jp/contests/code-thanks-festival-2017-open/tasks/code_thanks_festival_2017_h //https://atcoder.jp/contests/cf16-tournament-round1-open/tasks/asaporo_c //diameter //https://atcoder.jp/contests/agc033/tasks/agc033_c //descendant //https://atcoder.jp/contests/code-thanks-festival-2018/tasks/code_thanks_festival_2018_f //size // //eulerTour //https://yukicoder.me/problems/no/900 // Tree> tree(N); int main() { int N; cin >> N; Tree> tree(N); for(int i = 0; i < N-1; ++i){ int u,v,w; cin >> u >> v >> w; u--,v--; tree.makeEdge(u,v,w); tree.makeEdge(v,u,w); } tree.makeDepth(0); tree.makeChild(); tree.makeSize(); ll ans = 0; for(int pa:tree.order) for(auto e:tree.child[pa]) ans += e.second*tree.size[e.first]*(N-tree.size[e.first])*2LL; cout << ans << endl; return 0; return 0; }