#include #include #include #include #include std::vector>> T; std::map, long long> memo; long long dfs(int src, int me){ if(memo.count({src, me}) == 0){ long long w = 0, w_0 = 0; for(const auto & p : T[me]){ if(p.first == src){ w_0 = p.second; continue; } w = std::max(w, dfs(me, p.first)); } w = std::max(w + w_0, 0LL); memo.emplace(std::make_pair(src, me), w); } return memo.at({src, me}); } int main(){ int N, u, v, w; std::cin >> N; T.resize(N); for(int n = 1; n < N; ++n){ std::cin >> u >> v >> w; u -= 1, v -= 1; T[u].emplace_back(v, w); T[v].emplace_back(u, w); } long long ans = 0; for(int n = 0; n < N; ++n){ std::vector temp; for(const auto & p : T[n]){ temp.push_back(dfs(n, p.first)); } if(temp.size() == 1){ ans = std::max(ans, temp[0]); } else if(temp.size() > 1){ std::nth_element(temp.begin(), temp.begin() + 2, temp.end()); temp[1] = std::max(temp[1], 0LL); ans = std::max(ans, temp[0] + temp[1]); } } std::cout << ans << std::endl; }