#include using namespace std; using i64 = long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector>> edges(n); i64 maxEdge = 0; // for the case all weights are negative for(int i = 0; i < n - 1; ++i){ int u, v, w; cin >> u >> v >> w; --u; --v; edges[u].emplace_back(v, w); edges[v].emplace_back(u, w); maxEdge = max(maxEdge, (i64)w); } vector dpDown(n, -1e18); i64 ans = maxEdge; // at least the single largest edge if all are negative // dfs(u, parent): computes dpDown[u] and updates ans auto dfs = [&](auto&& self, int u, int p) -> void { // collect best "downward" contributions from children vector bests; for(auto [v, w] : edges[u]){ if(v == p) continue; self(self, v, u); // either extend through v, or take just the edge u-v i64 contrib = max(dpDown[v] + w, (i64)w); bests.push_back(contrib); } if(bests.empty()){ dpDown[u] = 0; return; } std::sort(bests.begin(), bests.end(), greater()); i64 best1 = bests[0]; i64 best2 = bests[1]; ans = max(ans, best1); ans = max(ans, best1 + best2); dpDown[u] = best1; }; dfs(dfs, 0, -1); cout << ans << "\n"; return 0; }