#include #include #include using int64 = long long; template using Graph = std::vector>; template class Edge { public: int to; T weight; Edge(int to, T weight) : to{to}, weight{weight} {} }; template class TreeDiameter { Graph> g; using Result = std::pair; Result dfs(int par, int curr) { Weight max_dist = std::numeric_limits::min(); int max_vertex = curr; bool has_child = false; for (auto& e : g[curr]) { int to = e.to; Weight weight = e.weight; if (to == par) continue; auto [dist, vertex] = dfs(curr, to); Weight v = dist + weight; if (!has_child || v > max_dist) { has_child = true; max_dist = v; max_vertex = vertex; } } if (!has_child) { max_dist = 0; max_vertex = curr; } return {max_dist, max_vertex}; } public: TreeDiameter(const Graph>& g) : g{g} {} Weight solve() { Result r = dfs(-1, 0); Result t = dfs(-1, r.second); return t.first; } }; int main() { int N; std::cin >> N; Graph> g(N); int64 u, v, w; for (int i = 0; i < N - 1; i++) { std::cin >> u >> v >> w; u--; v--; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } TreeDiameter tree(g); int64 res = tree.solve(); std::cout << std::max(0, res) << std::endl; return 0; }