#include using namespace std; using ll = long long; const int MAXN = 500005; vector> G[MAXN]; ll ans = LLONG_MIN; ll dfs(int v, int p) { ll max1 = 0, max2 = 0; // 部分木内の最大と2番目のパス長 for (auto [to, w] : G[v]) { if (to == p) continue; ll d = dfs(to, v) + w; if (d > max1) { max2 = max1; max1 = d; } else if (d > max2) { max2 = d; } } ans = max(ans, max1 + max2); // 途中で一番長いパスを更新 return max1; } int main() { int N; cin >> N; for (int i = 0; i < N - 1; ++i) { int u, v; ll w; cin >> u >> v >> w; G[u].emplace_back(v, w); G[v].emplace_back(u, w); } dfs(1, -1); cout << ans << endl; return 0; }