#include using namespace std; #define ll long long #define rep(i,n) for(int i=0;i #define riano_ std::ios::sync_with_stdio(false);std::cin.tie(nullptr) using Graph = vector>; multiset in,out; ll tmp = 2e18; //dfs //s:始点 i:dfs回数 vector vis; void dfs_insert(Graph &G, int s,int i,int fbd){ for(auto[nx,c]:G[s]){ if(vis[nx]==i) continue; if(nx==fbd) continue; vis[nx] = i; out.insert(c); dfs_insert(G,nx,i,fbd); } } void dfs_calc(Graph &G, int s,int i,int fbd){ auto itr = in.end(); itr--; ll y = *itr; itr = in.begin(); ll x = *itr; itr = out.end(); itr--; ll z = *itr; chmin(tmp,max(z,y-x)); for(auto[nx,c]:G[s]){ if(vis[nx]==i) continue; if(nx==fbd) continue; vis[nx] = i; out.erase(out.find(c)); in.insert(c); dfs_calc(G,nx,i,fbd); in.erase(in.find(c)); out.insert(c); } } int main() { riano_; ll ans = 0; ll N; cin >> N; Graph G(N+1); ll ma,mb,mc = 0; rep(i,N-1){ ll a,b,c; cin >> a >> b >> c; G[a].emplace_back(b,c); G[b].emplace_back(a,c); if(c>mc){ mc = c; ma = a; mb = b; } } vis.assign(N+1,-1); in.insert(mc); out.insert(0); vis[ma] = 0; dfs_insert(G,ma,0,mb); vis[ma] = 1; dfs_calc(G,ma,1,mb); chmax(ans,tmp); swap(ma,mb); in.clear(); out.clear(); tmp = 2e18; vis.assign(N+1,-1); in.insert(mc); out.insert(0); vis[ma] = 0; dfs_insert(G,ma,0,mb); vis[ma] = 1; dfs_calc(G,ma,1,mb); chmax(ans,tmp); cout << ans << endl; }