#P10418. [蓝桥杯 2023 国 A] 相连的边

[蓝桥杯 2023 国 A] 相连的边

Problem Description

Given a tree with nn nodes, find 33 edges such that the endpoints of these three edges can reach each other by traversing only these three edges, and the sum of the lengths of the three edges is as large as possible.

Input Format

The first line contains an integer nn, representing the number of nodes in the tree.

The next n1n - 1 lines each contain two integers separated by a single space. In the ii-th line, the two numbers xi,yix_i, y_i mean that there is an edge of length yiy_i between node i+1i + 1 and node xix_i.

Output Format

Output one line containing one integer, representing the maximum possible sum of the lengths of the three edges that satisfy the requirement.

5
1 1
2 1
3 7
1 10

12

Hint

[Sample Explanation 1]

The edge length between node 22 and node 11 is 11, the edge length between node 33 and node 22 is 11, the edge length between node 44 and node 33 is 77, and the edge length between node 55 and node 11 is 1010.

[Test Case Scale and Assumptions]

For 20%20\% of the test cases, 4n3004 \le n \le 300.
For 40%40\% of the test cases, 4n50004 \le n \le 5000.
For all test cases, 4n2×1054 \le n \le 2 \times 10^5, 1xi<i1 \le x_i < i, and 1yi1091 \le y_i \le 10^9.

Translated by ChatGPT 5