#P8258. [CTS2022] 独立集问题

[CTS2022] 独立集问题

Problem Description

Xiao E likes to create Maximum Weight Independent Set problems.

Next, he came up with nn Maximum Weight Independent Set problems.

But unlike before, this time he wants to merge these nn problems into a single problem.

Xiao E has nn AIs, numbered from 11 to nn.

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

Some AIs can communicate with each other. For all 2in2 \le i \le n, the ii-th AI can communicate directly with the cic_i-th AI, where ci<ic_i<i. Therefore, these AIs form a tree. In addition, any other pairs of AIs cannot communicate with each other directly.

Each time, Xiao E can choose an AI that currently stores a Maximum Weight Independent Set problem, and merge it with all problems on AIs that can communicate directly with it, forming a new Maximum Weight Independent Set problem. When merging, there is always a loss of difficulty, so it is impossible to simply add up all difficulties. The difficulty of the new Maximum Weight Independent Set problem equals the sum of the difficulties of the problems on those AIs that can communicate directly with it, minus the difficulty of the problem originally stored on the chosen AI. Then, the difficulties of the problems on those directly communicating AIs become 00.

Xiao E wants to perform several operations so that the problem difficulties on n1n-1 AIs all become 00, and then publish the final problem stored in the last remaining AI.

Due to the problem setter's twisted mindset, Xiao E wants the difficulty of the final Maximum Weight Independent Set problem to be as large as possible.

He asks you to help solve this problem, and says that if you succeed, then when publishing that Maximum Weight Independent Set problem, 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, where the ii-th one denotes did_i.

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

Output Format

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

4
-1 2 3 4
1 1 1

10

Hint

Constraints: 1n3514931\le n\le 351493.

Constraints: 1ci<i1\le c_i <i.

Constraints: di109|d_i|\le 10^9.

Subtask 1 (13 points): ci=i1c_i=i-1.

Subtask 2 (6 points): di>0d_i>0.

Subtask 3 (11 points): n7n\le 7.

Subtask 4 (13 points): n16n\le 16.

Subtask 5 (22 points): n100n\le 100.

Subtask 6 (35 points): no special properties.

Translated by ChatGPT 5