from collections import defaultdict import sys sys.setrecursionlimit(1 << 25) class WeightedTree: def __init__(self, n): self.n = n self.graph = defaultdict(list) self.max_path_sum = float('-inf') # グローバル最大値 def add_edge(self, u, v, w): self.graph[u].append((v, w)) self.graph[v].append((u, w)) def dfs(self, node, parent): max1 = 0 # 部分木の中で最も重みが大きいパス max2 = 0 # 2番目に大きいパス(直径は2つの枝が合流してできることがある) for neighbor, weight in self.graph[node]: if neighbor == parent: continue res = self.dfs(neighbor, node) + weight if res > max1: max2 = max1 max1 = res elif res > max2: max2 = res # 「このノードを中心としたパス」の最大値候補 self.max_path_sum = max(self.max_path_sum, max1 + max2) # 親に返すのは、「このノードから伸びる最大の片道パス長」 return max(max1, 0) # 0未満なら無視 def find_max_path_sum(self): self.dfs(1, -1) # ノード番号が1から始まると仮定 return self.max_path_sum # ----------- 入力例と実行 ----------- if __name__ == "__main__": N = int(input()) tree = WeightedTree(N) for _ in range(N - 1): u, v, w = map(int, input().split()) tree.add_edge(u, v, w) print(tree.find_max_path_sum())