#P9111. [福建省队集训2019] 最大权独立集问题

[福建省队集训2019] 最大权独立集问题

Problem Description

E.Space likes to create Maximum Weight Independent Set problems.

Next, he wants to create nn Maximum Weight Independent Set problems.

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

At the beginning, the ii-th AI stores one Maximum Weight Independent Set problem that E.Space prepared in advance, with difficulty did_i.

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 in both directions. Besides these, no other pairs of AIs can communicate.

Each time, E.Space can choose an AI that currently stores a Maximum Weight Independent Set problem, publish the problem stored in it, and then clear the problem stored in that AI.

After E.Space publishes the problem and before clearing it, the AI will send this problem to all AIs that can communicate with it.

If an AI that receives this problem already has a Maximum Weight Independent Set problem stored, then it will merge the received problem with the one it already has, and store a new Maximum Weight Independent Set problem. Formally, if it originally stored a problem with difficulty xx, and then receives a problem with difficulty yy, then after merging it becomes a problem with difficulty x+yx+y.

If an AI that receives the problem does not have any problem stored, then it will destroy the received information.

Due to the problem setter’s twisted mindset, E.Space wants the total difficulty sum of the nn Maximum Weight Independent Set problems he publishes to be as large as possible.

He wants you to help him solve this problem. He also says that if you successfully solve this problem in this training, then when publishing those nn Maximum Weight Independent Set problems, he will switch to your account in the last 10 minutes before the training ends and submit an official solution.

Input Format

The first line contains a positive integer nn.

The second line contains nn integers, where the ii-th one denotes did_i.

The third line contains n1n-1 integers, where the ii-th one denotes ci+1c_{i+1}.

Output Format

Output one integer in one line, denoting the maximum possible total difficulty sum.

4
1 10 3 5
1 2 3

52

4
1 -2 5 5
1 2 2

27

Hint

Sample Explanation 1

One optimal publishing plan is as follows:

  1. Publish the Maximum Weight Independent Set problem in the 22-nd AI. The difficulty of this problem is 1010. After publishing, the difficulties of the problems stored in the 44 AIs are 11,,13,511,*,13,5 (* means that this AI has no Maximum Weight Independent Set problem stored, same below).

  2. Publish the Maximum Weight Independent Set problem in the 33-rd AI. The difficulty of this problem is 1313. After publishing, the difficulties of the problems stored in the 44 AIs are 11,,,1811,*,*,18.

  3. Publish the Maximum Weight Independent Set problem in the 11-st AI. The difficulty of this problem is 1111. After publishing, the difficulties of the problems stored in the 44 AIs are ,,,18*,*,*,18.

  4. Publish the Maximum Weight Independent Set problem in the 44-th AI. The difficulty of this problem is 1818. After publishing, the difficulties of the problems stored in the 44 AIs are ,,,*,*,*,*.

So the total difficulty sum of the 44 problems is 10+13+11+18=5210+13+11+18=52.

Sample Explanation 2

One optimal publishing plan is as follows:

  1. Publish the Maximum Weight Independent Set problem in the 33-rd AI. The difficulty of this problem is 55. After publishing, the difficulties of the problems stored in the 44 AIs are 1,3,,51,3,*,5.

  2. Publish the Maximum Weight Independent Set problem in the 44-th AI. The difficulty of this problem is 55. After publishing, the difficulties of the problems stored in the 44 AIs are 1,8,,1,8,*,*.

  3. Publish the Maximum Weight Independent Set problem in the 22-nd AI. The difficulty of this problem is 88. After publishing, the difficulties of the problems stored in the 44 AIs are 9,,,9,*,*,*.

  4. Publish the Maximum Weight Independent Set problem in the 11-st AI. The difficulty of this problem is 99. After publishing, the difficulties of the problems stored in the 44 AIs are ,,,*,*,*,*.

So the total difficulty sum of the 44 problems is 5+5+8+9=275+5+8+9=27.

Constraints

It is guaranteed that di109\left|d_i\right| \le 10^9, 1ci<i1 \le c_i \lt i, and 1n4001 \le n \le 400.

This problem uses bundled tests.

For subtasks with odd indices, it is guaranteed that di0d_i \ge 0.

Subtasks 1,21,2 (11×2=2211 \times 2 = 22 points): n9n \le 9.

Subtasks 3,43,4 (10×2=2010 \times 2 = 20 points): n19n \le 19.

Subtasks 5,65,6 (7×2=147 \times 2 = 14 points): n50n \le 50, ci=i1c_i = i-1.

Subtasks 7,87,8 (10×2=2010 \times 2 = 20 points): ci=i1c_i=i-1.

Subtasks 9,109,10 (5×2=105 \times 2 = 10 points): n50n \le 50.

Subtasks 11,1211,12 (7×2=147 \times 2 = 14 points): No special constraints.

Afterword

It is said that the difficulty of E.Space’s Maximum Weight Independent Set problems is taken as a logarithm?

It is said that E.Space wants to merge these nn problems into one single problem and publish it?

It is said that E.Space will not put these problems in the training?

Translated by ChatGPT 5