import sys from collections import deque sys.setrecursionlimit(1 << 25) def main(): input = sys.stdin.read().split() idx = 0 n = int(input[idx]) idx += 1 adj = [[] for _ in range(n + 1)] for _ in range(n - 1): u = int(input[idx]) v = int(input[idx + 1]) w = int(input[idx + 2]) idx += 3 adj[u].append((v, w)) adj[v].append((u, w)) max_in_subtree = [-float('inf')] * (n + 1) max_single = [0] * (n + 1) stack = [(1, -1, False)] while stack: u, parent, visited = stack.pop() if not visited: stack.append((u, parent, True)) for v, w in adj[u]: if v != parent: stack.append((v, u, False)) else: max1 = -float('inf') max2 = -float('inf') current_max_in_subtree = -float('inf') for v, w in adj[u]: if v == parent: continue current_child_in = max_in_subtree[v] current_child_single = max_single[v] if current_child_in > current_max_in_subtree: current_max_in_subtree = current_child_in current_single = current_child_single + w if current_single > max1: max2 = max1 max1 = current_single elif current_single > max2: max2 = current_single current_max = -float('inf') if max1 != -float('inf') and max2 != -float('inf'): current_max = max1 + max2 if max1 != -float('inf'): current_max = max(current_max, max1) max_in_subtree[u] = max(current_max_in_subtree, current_max) max_single_val = max(0, max1) if max1 != -float('inf') else 0 max_single[u] = max_single_val print(max(max_in_subtree[1], 0)) if __name__ == '__main__': main()