#P15454. [JOI 2026 SemiFinal] 川下り / River Rafting

[JOI 2026 SemiFinal] 川下り / River Rafting

Problem Description

In the country of JOI, there are NN towns, numbered from 11 to NN. Between these towns there are N−1N - 1 roads, numbered from 11 to N−1N - 1. Road ii (1≤i≤N−11 \le i \le N - 1) bidirectionally connects town PiP_i (Pi≤iP_i \le i) and town i+1i + 1. It is guaranteed that starting from town 11, you can reach any town by traveling along some roads.

In addition, JOI has N−1N - 1 rivers parallel to the roads, numbered from 11 to N−1N - 1. River ii (1≤i≤N−11 \le i \le N - 1) is parallel to road ii, and flows from town PiP_i to town i+1i + 1.

Each of the NN towns has a lamp. Each lamp has an intensity. If the lamp at town tt (1≤t≤N1 \le t \le N) has intensity ll, then all towns that can be reached from town tt by traveling along fewer than ll roads are illuminated by this lamp. Initially, all lamps have intensity 00, and no town is illuminated.

You may perform the operation “going downstream” any number of times (including 00 times). Each time you go downstream, you start from being at town 11, and first increase the lamp intensity at town 11 by 11. Then, you repeatedly perform the following steps in order:

  1. Decide whether to end this downstream trip. However, if there is no outgoing river from the current town, you must end.
  2. If you continue going downstream, choose one river among the rivers flowing out of the current town, and move along that river. After moving, increase the lamp intensity at the town you arrive at by 11.

If you end the downstream trip at town tt, then the cost of this downstream trip is CtC_t. You want to perform some number of downstream trips so that every town is illuminated by at least one lamp. Under this condition, minimize the total cost of all downstream trips.

Input Format

Input is given from standard input in the following format:

NN
P1 P2 ⋯ PN−1P_1\ P_2\ \cdots\ P_{N-1}
C1 C2 ⋯ CNC_1\ C_2\ \cdots\ C_N

Output Format

Print, in one line, the minimum possible total cost of downstream trips required to make every town illuminated by at least one lamp.

5
1 2 2 4
10 4 8 9 5
9
9
1 1 1 2 5 5 5 3
100 70 80 90 60 30 40 50 30
90

Hint

Sample Explanation 1

In the first downstream trip, choose river 11 and end at town 22. Then, the lamp intensities of towns 1,21, 2 each increase by 11, and the cost is 44. In the second downstream trip, choose rivers 1,3,41, 3, 4 and end at town 55. Then, the lamp intensities of towns 1,2,4,51, 2, 4, 5 each increase by 11, and the cost is 55.

After these operations, the lamp intensity of towns 1,21, 2 is 22, the lamp intensity of town 33 is 00, and the lamp intensity of towns 4,54, 5 is 11. Towns 1,2,3,41, 2, 3, 4 are illuminated by the lamp at town 22 with intensity 22, and town 55 is illuminated by the lamp at town 55 with intensity 11. Therefore, after these operations, all towns are illuminated by some lamp. The total cost is 4+5=94 + 5 = 9. It is impossible to satisfy the condition with a cost smaller than 99, so output 99.

This input sample satisfies the Constraints of subtasks 1,2,5,61, 2, 5, 6.

Sample Explanation 2

Perform two downstream trips that go along rivers 1,4,51, 4, 5 and end at town 66, and one downstream trip that goes along rivers 2,82, 8 and ends at town 99. After these operations, the lamp intensity of town 11 is 33; the lamp intensities of towns 2,5,62, 5, 6 are 22; the lamp intensities of towns 3,93, 9 are 11; and the lamp intensities of towns 4,7,84, 7, 8 are 00. Through these operations, all towns are illuminated by some lamp. The total cost is 30×2+30=9030 \times 2 + 30 = 90. It is impossible to satisfy the condition with a cost smaller than 9090, so output 9090.

This input sample satisfies the Constraints of subtasks 2,62, 6.

Constraints

  • 2≤N≤7002 \le N \le 700
  • 1≤Pi≤i1 \le P_i \le i (1≤i≤N−11 \le i \le N-1)
  • 1≤Ct≤1091 \le C_t \le 10^9 (1≤t≤N1 \le t \le N)
  • All input values are integers.

Subtasks

  1. (13 points) N≤8N \le 8
  2. (25 points) N≤100N \le 100
  3. (7 points) Pi=1P_i = 1 (1≤i≤N−11 \le i \le N-1)
  4. (11 points) Pi=iP_i = i (1≤i≤N−11 \le i \le N-1)
  5. (16 points) For each ii (1≤i≤N1 \le i \le N), the number of jj (1≤j≤N−11 \le j \le N-1) such that Pj=iP_j = i is at most 22
  6. (28 points) No additional constraints

Translated by DeepSeek.

Translated by ChatGPT 5