QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 2048 MB 總分: 100

#4054. Maximum Weight Independent Set Problem

统计

Little E likes to create Maximum Weight Independent Set problems. Next, he has thought of $n$ Maximum Weight Independent Set problems. Little E has $n$ AIs, numbered $1 \sim n$. Initially, the $i$-th AI contains $d_i$ Maximum Weight Independent Set problems that Little E has prepared in advance. Some AIs can communicate with each other. For all $2 \le i \le n$, the $i$-th AI can communicate with the $c_i$-th AI. Here $c_i < i$, and the same $c_i$ appears at most 2 times. Therefore, these AIs form the shape of a binary tree. Furthermore, no other pairs of AIs can communicate with each other. Little E needs to temporarily disconnect the connections between these AIs. He can only disconnect the connections between AIs one by one. Before two AIs that were originally able to communicate have their connection disconnected, they will exchange all the problems stored within them; please see the example for details. Little E wants to minimize the total number of problems involved in exchanges after all connections are broken. He wants you to help him solve this problem, and he says that if you successfully solve this problem, he will help you submit a reference solution code when he creates those Maximum Weight Independent Set problems.

Input

The first line contains a positive integer $n$. The second line contains $n$ integers, where the $i$-th integer represents $d_i$. The third line contains $n - 1$ positive integers, where the $i$-th integer represents $c_{i+1}$.

Output

A single integer representing the answer.

Examples

Input 1

3
2 1 3
1 1

Output 1

7

Note

One optimal strategy is: disconnect the connection between AI 1 and AI 2, which requires exchanging $2 + 1 = 3$ problems; then disconnect the connection between AI 1 and AI 3, which requires exchanging $1 + 3 = 4$ problems. Therefore, the answer is 7.

Subtasks

It is guaranteed that $1 \le c_i < i$, and the same $c_i$ appears at most 2 times. It is guaranteed that $1 \le d_i \le 10^9$.

Test Case ID $n \le$
$1 \sim 3$ 10
$4 \sim 7$ 100
$8 \sim 11$ 500
$12 \sim 16$ 1000
$17 \sim 25$ 5000

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.