#include #include #include #include #include #include #include #include using namespace std; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; const long long MOD = 1e9+7; struct Edge { int u, v; ll w; }; int max_v = 200000; vector> graph(max_v); vector cnt_child(max_v); vector done(max_v); int dfs(int v) { if (done.at(v)) return 0; done.at(v) = true; int res = 1; for (int to: graph.at(v)) res += dfs(to); return cnt_child.at(v) = res; } int main() { int n; cin >> n; vector edges(n - 1); for (int i = 0; i < n - 1; i++) { int u, v; ll w; cin >> u >> v >> w; u--; v--; graph.at(u).push_back(v); graph.at(v).push_back(u); edges.at(i) = Edge{u, v, w}; } dfs(0); ll sum = 0; for (Edge e: edges) { ll m = min(cnt_child.at(e.u), cnt_child.at(e.v)); sum += m * (n - m) * e.w; } cout << sum * 2 << endl; }