#include #include #include using namespace std; typedef long long ll; struct Edge { int to; ll weight; }; vector> adj; ll ans = 0; ll dfs(int u, int p) { ll max1 = 0; ll max2 = 0; for (const auto& edge : adj[u]) { int v = edge.to; ll w = edge.weight; if (v == p) continue; ll path_from_v_subtree = dfs(v, u); ll current_path_through_v = 0; if (path_from_v_subtree + w > 0) { current_path_through_v = path_from_v_subtree + w; } if (current_path_through_v >= max1) { max2 = max1; max1 = current_path_through_v; } else if (current_path_through_v > max2) { max2 = current_path_through_v; } } ans = max(ans, max1 + max2); return max1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; adj.resize(n); for (int i = 0; i < n - 1; ++i) { int u_node, v_node; ll w; cin >> u_node >> v_node >> w; --u_node; --v_node; adj[u_node].push_back({v_node, w}); adj[v_node].push_back({u_node, w}); } if (n == 1) { cout << 0 << endl; return 0; } dfs(0, -1); cout << ans << endl; return 0; }