#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 = 0; int max_vertex = curr; for (const auto& [to, weight] : g[curr]) { if (to == par) continue; auto [dist, vertex] = dfs(curr, to); Weight value = dist + weight; if (value > max_dist) { max_dist = value; max_vertex = vertex; } } 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); std::cout << tree.solve() << std::endl; return 0; }