#P17138. [KOI 2026 #1] 步道

    ID: 19481 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>Special Judge2026KOI(韩国)

[KOI 2026 #1] 步道

Problem Description

There are NN rest stops on KOI Mountain, numbered from 11 to NN, and N−1N-1 undirected trails connecting these rest stops.

For each integer ii (1≤i≤N1 \le i \le N), the crowding level of rest stop ii is given by a positive integer AiA_i.

For each integer jj (1≤j≤N−11 \le j \le N-1), trail jj connects rest stop jj and rest stop CjC_j (j+1≤Cj≤Nj+1 \le C_j \le N), and its length is LjL_j. In other words, all rest stops are connected by these trails to form a tree.

The lonely hiker Gyojun wants to choose two different rest stops and walk along the unique simple path between them.

For a chosen path, define:

  • SS as the sum of the lengths of all trails on the path.
  • MM as the maximum crowding level among all rest stops on the path.

Gyojun wants to walk as far as possible and also enjoy a quiet walk where there are fewer people, so he defines the satisfaction of this path as S−MS-M.

Write a program to determine which two rest stops Gyojun should choose to maximize the satisfaction of the walking path.

Input Format

The first line contains an integer NN.

The second line contains NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N, separated by spaces.

The third line contains N−1N-1 integers C1,C2,…,CN−1C_1,C_2,\ldots,C_{N-1}, separated by spaces.

The fourth line contains N−1N-1 integers L1,L2,…,LN−1L_1,L_2,\ldots,L_{N-1}, separated by spaces.

Output Format

Output two different rest stop indices on the first line, separated by a space, such that the satisfaction of the path between them is maximized.

If there are multiple valid answers, output any one of them.

2
1 2
2
1
1 2
4
6 10 20 1
2 4 4
3 21 15
1 3

Hint

Sample Explanation 1

The satisfaction of the path between rest stop 11 and rest stop 22 is −1-1, and this value is the maximum among all path satisfactions.

Sample Explanation 2

The satisfactions of all possible paths are as follows:

  • The satisfaction of path 1−21-2 is 3−max⁡{6,10}=3−10=−73-\max\{6,10\}=3-10=-7.
  • The satisfaction of path 1−2−4−31-2-4-3 is (3+21+15)−max⁡{6,10,20,1}=39−20=19(3+21+15)-\max\{6,10,20,1\}=39-20=19.
  • The satisfaction of path 1−2−41-2-4 is (3+21)−max⁡{6,10,1}=24−10=14(3+21)-\max\{6,10,1\}=24-10=14.
  • The satisfaction of path 2−4−32-4-3 is (21+15)−max⁡{10,20,1}=36−20=16(21+15)-\max\{10,20,1\}=36-20=16.
  • The satisfaction of path 2−42-4 is 21−max⁡{10,1}=21−10=1121-\max\{10,1\}=21-10=11.
  • The satisfaction of path 3−43-4 is 15−max⁡{20,1}=15−20=−515-\max\{20,1\}=15-20=-5.

Constraints

  • All numbers in the input are integers.
  • 2≤N≤300 0002 \le N \le 300\,000.
  • For each integer ii (1≤i≤N1 \le i \le N), 1≤Ai≤10181 \le A_i \le 10^{18}.
  • For each integer jj (1≤j≤N−11 \le j \le N-1), j+1≤Cj≤Nj+1 \le C_j \le N and 1≤Lj≤10121 \le L_j \le 10^{12}.

Subtasks

  1. (88 points) N≤300N \le 300.
  2. (1212 points) N≤7 500N \le 7\,500.
  3. (1111 points) A1=A2=⋯=ANA_1=A_2=\cdots=A_N.
  4. (1515 points) For each integer jj (1≤j≤N−11 \le j \le N-1), Cj=j+1C_j=j+1.
  5. (1717 points) C1=C2=⋯=CN−1=NC_1=C_2=\cdots=C_{N-1}=N.
  6. (3636 points) The number of distinct integers in the set {A1,A2,…,AN}\{A_1,A_2,\ldots,A_N\} is at most 2020.
  7. (5151 points) No additional constraints.

Translated by ChatGPT-5.6.

Translated by ChatGPT 5