Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.
Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?
Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends a, b know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.
Jenny is given the number 0.
Input
The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers u, v and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.
It is guaranteed that the social network of the input forms a tree.
Output
Output a single integer – the maximum sum of costs.
In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand problem: Heidi's friend Jenny asks her to deliver an important letter to one of their common friends. The suspicion: it's a prank where the message says "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver same message to another friend (that the recipient has in common with Heidi), and so on. Heidi will not visit same person twice (including Jenny). So the sequence is a simple path starting from Jenny (node 0) visiting distinct nodes, each next node must be adjacent to current node and not visited before. At each step, the current friend tells her to go to another friend (adjacent) that hasn't been visited (including possibly going back? No, cannot revisit). Since it's a tree, any simple path is allowed. The cost is sum of traveling costs along edges used. She wants to know the maximal amount of money she will waste on travel if it is a prank. That is, the maximum possible total cost of a simple path starting at node 0, where she keeps moving to an unvisited neighbor (i.e., a path in tree starting at root). But note: The process stops when? She delivers message to someone who doesn't have any common friend with Heidi that hasn't been visited? But in the prank, each recipient sends her to another friend (that the recipient has in common with Heidi). However, if a node has degree 1 (except maybe root) and the only neighbor is already visited, then it cannot send her further. In the examples, the worst-case path ends at a leaf? Actually in a tree, if you start at root 0, and you always go to an unvisited neighbor, the path is a simple path from root to some leaf. But note: When you are at a node, you might have multiple unvisited neighbors; the friend (the current node) chooses which one to send her to (the prank). Heidi wants worst-case total travel cost, i.e., the maximum possible sum over such a process, assuming the friends (prank) will send her in such a way to maximize total travel cost? Actually statement: "She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?" Interpretation: In the prank scenario, the message says "Send the fool further!", and the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on. Heidi will not be made to visit the same person twice. She wants to know maximal amount of money she will waste on travel if it really is a prank. This suggests that the friends could possibly choose which neighbor to send her to, and Heidi wants to consider the worst-case (maximum total cost) that could happen, given that the process continues until she cannot be sent further (i.e., she reaches a leaf) or maybe stops earlier if they choose to? But the wording "maximal amount of money she will waste on travel if it really is a prank" implies that if it is a prank, she might end up traveling some cost; but perhaps the friends could choose different recipients to maximize the cost (since it's a prank, they'd want to maximize her travel). So we need to compute the maximum total cost of a simple path starting at node 0, where at each step, the current node chooses an unvisited neighbor to go to, and she must travel the edge cost, and then continues. The process ends when the current node has no unvisited neighbors (i.e., it's a leaf in the subtree of visited nodes). Because she cannot revisit. In a tree, starting at 0, the set of visited nodes will be a simple path. Since it's a tree, if you never backtrack, the visited nodes are exactly a simple path from 0 to some node. Because if you ever go to a node with degree > 2, you might have a choice but you can only go forward, not backtrack. However, can you backtrack? The rules: "She will not be made to visit the same person twice." So she cannot go back to a visited node. If she is at a node, she can only go to an adjacent unvisited node. In a tree, if she starts at 0 and moves along edges never revisiting, the visited nodes form a simple path from 0 to the current node. There's no way to branch, because once you leave a node, you can't come back. So the entire walk is a simple path. At each step, the friend (current node) can choose any neighbor that hasn't been visited. Since 0 is visited at start, from 0 she can go to any neighbor. Then from that neighbor, she can go to any neighbor except 0 (visited). So essentially, the path is a simple path starting at 0. The maximum total cost is the maximum sum of edge weights along any simple path starting at 0. BUT is that correct? Let's test with examples.
Example 1:
n=4
edges: 0-1 cost4, 0-2 cost2, 2-3 cost3.
All simple paths from 0: 0-1 cost4; 0-2 cost2; 0-2-3 cost2+3=5. So maximum=5, matches output.
Example2:
n=6
edges:
1-2 cost3
0-2 cost100
1-4 cost2
0-3 cost7
3-5 cost10
Tree: Let's parse: nodes 0,1,2,3,4,5.
Edges: 1-2 (3), 0-2 (100), 1-4 (2), 0-3 (7), 3-5 (10).
Draw: 0 connected to 2 (100) and 3 (7). 1 connected to 2 (3) and 4 (2). 2 connected to 0 and 1. 3 connected to 0 and 5. 4 connected to 1. 5 connected to 3.
Starting from 0, possible simple paths:
0-2 (100)
0-2-1 (100+3=103)
0-2-1-4 (100+3+2=105)
0-3 (7)
0-3-5 (7+10=17)
0-2-1-4 seems to be max=105, matches output.
0-2-9: 881+1536=2417.
Maximum among these: 5551 (0-2-3). Output is 5551. So indeed maximum sum path from root to any node.
Thus the problem reduces to: Given a tree with n nodes, node 0 is the root. Edge weights positive. Find the maximum total weight from node 0 to any node (i.e., the maximum distance from root). This is simply the farthest node distance in terms of sum of edge weights.
Wait, but is it always just root-to-leaf path? Yes because any simple path from root is just a root-to-node path. The maximum will be to some leaf, but not necessarily leaf? Could be internal node if leaf distances are smaller? But we can just compute distances from root via DFS/BFS and take max.
But is there any nuance about "She will not be made to visit the same person (including Jenny) twice." That's exactly a simple path.
Thus answer = maximum distance from node 0 to any other node.
Constraints: n ≤ 100, edge costs ≤ 10000. So simple.
Thus solution: read n, then for i from 1 to n-1 read u v c. Build adjacency list. DFS from 0 compute total distance to each node, track max.
But wait: Example1: path 0-2-3 sum=5, leaf 3. Example2: path 0-2-1-4 sum=105, leaf 4. Example3: path 0-2-3 sum=5551, leaf 3? Node 3 is leaf? In that tree node 3 has only connection to 2, so yes leaf.
Thus it's the maximum root-to-leaf distance.
Hence the solution is straightforward.
But is there any twist? Possibly the process could involve branching? For instance, could she be sent from node 0 to node A, then from A back to node 0? Not allowed because cannot revisit. So no.
Thus answer is longest path starting from root.
Thus we'll code.
But before finalize, consider the possibility that the path might not be simple root-to-leaf if there are nodes with degree >2, but she could go from root to a node, then to another child, etc. That's still root-to-leaf.
So we'll solve.
Implementation: Use adjacency list with (neighbor, weight). DFS or BFS.
Let's write Python code:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
adj = [[] for _ in range(n)]
for _ in range(n-1):
u = int(next(it)); v = int(next(it)); c = int(next(it))
adj[u].append((v, c))
adj[v].append((u, c))
# BFS/DFS from 0
max_dist = 0
stack = [(0, -1, 0)] # node, parent, dist
while stack:
node, parent, dist = stack.pop()
max_dist = max(max_dist, dist)
for neigh, w in adj[node]:
if neigh != parent:
stack.append((neigh, node, dist + w))
print(max_dist)
That's it.
But wait: Check example 2: n=6, edges: as above, BFS from 0 yields distances: dist[2]=100, dist[3]=7, dist[1]=103, dist[4]=105, dist[5]=17. max=105, correct.
Thus answer.
But is there any scenario where the maximum is not root-to-leaf? Suppose we have root with two long branches; maybe they can go root -> branch1 -> back? Not allowed.
Thus answer is longest root-to-node distance.
Double-check problem statement: "Heidi will not be made to visit the same person (including Jenny) twice." So indeed path is simple.
Thus solution is trivial.
But note that n is small, any approach works.
Thus final answer.
But wait: There is nuance: "She also knows how much it costs to travel between any two of her friends who know each other directly." So cost is symmetric.
Alright.
Thus final solution.
Now, let's produce final answer in the required format: a Python function or program.
But wait: Could there be a scenario where the worst-case is not simply the farthest node? Because the friends could decide to stop earlier? But they want to prank her, so they would likely try to maximize cost. But the message says "Send the fool further!" So each friend must send her to another friend if possible. At a leaf, there's no unvisited neighbor (except parent which is visited), so they can't send her further. So the process stops naturally. So they must continue as long as possible. So they would choose the path that maximizes total cost? However, each friend individually might not know the global structure; but the problem asks for maximal amount she will waste, assuming it is a prank. It might be that the friends collectively could choose the worst path. So we consider the maximum possible total cost from any possible path starting at 0, respecting no revisits. That's exactly longest path from root.
Thus answer.
I'll produce final code.
But wait: Could the path be not necessarily root-to-leaf? Suppose a node has multiple unvisited children, and from there she goes to one child; that child may have multiple children, etc. So it's still root-to-leaf in the tree.
Thus solution.
Now, I'll write the final answer as a Python script.
We'll include a main guard.
Thus final answer:
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
adj = [[] for _ in range(n)]
idx = 1
for _ in range(n-1):
u = int(data[idx]); v = int(data[idx+1]); c = int(data[idx+2])
idx += 3
adj[u].append((v, c))
adj[v].append((u, c))
max_dist = 0
stack = [(0, -1, 0)]
while stack:
node, parent, dist = stack.pop()
if dist > max_dist:
max_dist = dist
for nxt, w in adj[node]:
if nxt != parent:
stack.append((nxt, node, dist + w))
print(max_dist)
if name == "main":
solve()
That's it.
But maybe we can use recursion with sys.setrecursionlimit, but stack iterative is fine.
We'll produce this as final.
Now the answer: "the maximum sum of costs" = farthest distance from node 0.
Thus final.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
adj = [[] for _ in range(n)]
idx = 1
for _ in range(n - 1):
u = int(data[idx]); v = int(data[idx + 1]); c = int(data[idx + 2])
idx += 3
adj[u].append((v, c))
adj[v].append((u, c))
code
max_dist = 0
# (node, parent, distance_from_root)
stack = [(0, -1, 0)]
while stack:
node, parent, dist = stack.pop()
max_dist = max(max_dist, dist)
for nxt, w in adj[node]:
if nxt != parent:
stack.append((nxt, node, dist + w))
print(max_dist)