#include #include #include int main() { int n; std::cin >> n; std::vector adj(n, std::vector>()); for(int i = 0; i < n-1; i++) { int u, v, w; std::cin >> u >> v >> w; u--; v--; adj[u].emplace_back(w, v); adj[v].emplace_back(w, u); } auto dfs = [&](auto&& self, int v, int p) -> std::pair { // {自分を端に含むもの, 含まないもの} std::pair res = {0, 0}; std::vector children_score; for(auto [w, u] : adj[v]) { if(u == p) continue; auto [contain, not_contain] = self(self, u, v); res.first = std::max(res.first, contain + w); children_score.push_back(contain + w); res.second = std::max(res.second, not_contain); } std::sort(children_score.rbegin(), children_score.rend()); if(children_score.size() >= 1) res.second = std::max(res.second, children_score[0]); if(children_score.size() >= 2) res.second = std::max(res.second, children_score[0] + children_score[1]); return res; }; auto [contain, not_contain] = dfs(dfs, 0, -1); std::cout << std::max(contain, not_contain) << std::endl; }