#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) { Result ret(-1e18, curr); for (const auto& [to, weight] : g[curr]) { if (to == par) continue; Result t = dfs(curr, to); t.first += weight; ret = std::max(ret, t); } if (ret.first == -1e18) ret.first = 0; return ret; } 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); int u, v; int64 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; }