#P8294. [省选联考 2022] 最大权独立集问题

    ID: 9379 远端评测题 1000ms 2048MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>各省省选2022O2优化树形 DP凸完全单调性(wqs 二分)

[省选联考 2022] 最大权独立集问题

Problem Description

Little E likes to create maximum weight independent set problems.

Next, he came up with nn maximum weight independent set problems.

Little E has nn AIs, numbered 1n1 \sim n.

At the beginning, the ii-th AI stores did_i maximum weight independent set problems that Little E prepared in advance.

Some AIs can communicate with each other. For all 2in2 \le i \le n, the ii-th AI can communicate with the cic_i-th AI. Here ci<ic_i < i, and the same value of cic_i appears no more than 22 times. Therefore, these AIs form a binary tree. Besides these, no other pairs of AIs can communicate.

Little E needs to temporarily disconnect the connections among these AIs. He can only disconnect the connections one by one. Before disconnecting the connection between two AIs that can communicate, they will exchange all problems stored inside them; see the sample for details.

Little E wants the total number of problems involved in exchanges to be as small as possible after all connections are disconnected. He asks you to solve this problem, and says that if you succeed, then when creating those maximum weight independent set problems, he will help you submit a reference solution code.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers; the ii-th one is did_i.

The third line contains n1n - 1 positive integers; the ii-th one is ci+1c_{i+1}.

Output Format

Output one integer in a single line, representing the answer.

3
2 1 3
1 1
7

Hint

[Sample Explanation #1]

One optimal plan is: disconnect the connection between AI 11 and AI 22. Then they need to exchange 2+1=32 + 1 = 3 problems. Then disconnect the connection between AI 11 and AI 33. Then they need to exchange 1+3=41 + 3 = 4 problems. So the answer is 77.

[Constraints]

It is guaranteed that 1cii1 \le c_i \le i, and the same value of cic_i appears at most twice.

It is guaranteed that 1di1091 \le d_i \le {10}^9.

Test Point ID nn \leq
131 \sim 3 1010
474 \sim 7 100100
8118 \sim 11 500500
121612 \sim 16 10001000
172517 \sim 25 50005000

Translated by ChatGPT 5