#ifndef HIDDEN_IN_VS // 折りたたみ用 #include using namespace std; // 型名の短縮 using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9 * 10^18(int は -2^31 ~ 2^31 = 2 * 10^9) using pii = pair; using pll = pair; using pil = pair; using pli = pair; using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; using vb = vector; using vvb = vector; using vvvb = vector; using vc = vector; using vvc = vector; using vvvc = vector; using vd = vector; using vvd = vector; using vvvd = vector; template using priority_queue_rev = priority_queue, greater>; template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } #define all(a) a.begin(),a.end() #define rep(i, n) for(int i = 0, i##_end = int(n); i < i##_end; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0; set < (1 << int(d)); ++set) // d ビット全探索(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define smod(n, m) ((((n) % (m)) + (m)) % (m)) // 非負mod #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 // template inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod long long TEN(int x) { return x == 0 ? 1 : TEN(x - 1) * 10; } #include using namespace atcoder; // using mint = modint998244353; #endif #ifdef local_mode #include "local.hpp" #else #define dump(...) #define dumpel(v) #endif struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } } fastIOtmp; int main(){ int n; cin >> n; vector>> G(n); // vvi I(n); rep(i,n-1){ int u,v; ll w; cin >> u >> v >> w; u--;v--; G[u].push_back({v,w}); G[v].push_back({u,w}); } dumpel(G); vvl dp(n); rep(i,n) dp[i].resize(G[i].size()); vi IDX(n,-1); function dfs = [&](int u, int p, int idx){ bool is_leaf = true; ll rw; rw=0; rep(i,G[u].size()){ auto[v,w] = G[u][i]; if(v==p){ IDX[u]=i; continue; } is_leaf = false; dfs(v,u,i); chmax(rw,dp[u][i]+w); } if(p!=-1)dp[p][idx]=rw; return; }; dfs(0,-1,-1); ll inf = 4004004004004004004; dump("IDX:",IDX); vl dp2(n); function dfs2 = [&](int u, int p){ // int idx = -1; dump(u); vl V; rep(i,G[u].size()){ auto[v,w] = G[u][i]; // if(v==p){ // idx = i; // V.push_back(-inf); // continue; // } V.push_back(dp[u][i]+w); } dump(V); int m = V.size(); vl L(m+1,-inf),R(m+1,-inf); rep(i,m){ L[i+1] = max(L[i],V[i]); R[m-1-i] = max(R[m-i],V[m-1-i]); } dp2[u]=R[0]; vl M(m); rep(i,m){ M[i] = max(L[i],R[i+1]); } dump("M:",M); rep(i,G[u].size()){ auto[v,w] = G[u][i]; if(v==p)continue; dp[v][IDX[v]]=M[i]; dfs2(v,u); } return; }; dfs2(0,-1); dumpel(dp); cout << *max_element(all(dp2)) << endl; }