#include #include #include using namespace std; using ll = long long; const ll NEG_INF = -1e18; struct Edge { int to; ll cost; }; int N; vector> tree; ll answer = NEG_INF; ll dfs(int node, int parent) { ll max1 = 0, max2 = 0; for (const auto& edge : tree[node]) { if (edge.to == parent) continue; ll childLen = dfs(edge.to, node) + edge.cost; if (childLen > max1) { max2 = max1; max1 = childLen; } else if (childLen > max2) { max2 = childLen; } } // 更新:このノードを通る直径候補 answer = max(answer, max1 + max2); // このノードから上へ渡す最大長 return max1; } int main() { cin >> N; tree.resize(N); for (int i = 0; i < N - 1; ++i) { int a, b; ll cost; cin >> a >> b >> cost; --a; --b; // 1-indexed -> 0-indexed tree[a].push_back({b, cost}); tree[b].push_back({a, cost}); } dfs(0, -1); cout << answer << endl; return 0; }