#P7925. 「EVOI-RD2」童年
「EVOI-RD2」童年
Background
On the banyan tree by the pond, cicadas keep calling out summer.
On the swing by the playground, only butterflies rest on it.
On the blackboard, the teacher’s chalk keeps squeaking and writing nonstop.
Waiting for the bell, waiting for school to end.
Waiting for a childhood of games.
Problem Description
Charlie loved climbing trees when he was a child.
One day, Charlie decided to challenge a tall apple tree. This apple tree has nodes, where node is the root.
Each node contains either some apples or one bird nest. If the node has some apples, Charlie will pick all the apples and put them into his pocket. If the node is a bird nest and Charlie visits it for the first time, then Charlie will give one apple to each bird in the nest don’t ask why birds like apples.
In particular, if Charlie does not have enough apples in his pocket to give one to every bird at that node, then he will not go to that node. Note that when Charlie passes through a node multiple times, he will not pick apples again, and he will not give apples again.
Initially, Charlie has apples in his pocket. Charlie will start climbing from the root. Each time he passes along an edge to reach a node, he performs the corresponding operation (picking apples or giving apples; the operation at the root also counts). Charlie wants to maximize the number of apples he has in the end. Since Charlie is busy climbing other trees, he asks you to write a program to help him.
Input Format
The first line contains two integers , as described above.
The next line contains integers. The -th integer represents the parent of node .
The next line contains integers. The -th number is . If , then this node has apples. If , then this node has a bird nest with birds. If , then this node has nothing.
Output Format
Output one integer in one line, representing the maximum number of apples Charlie can have in the end.
5 0
1 1 2 2
1 1 1 1 1
5
5 0
1 1 2 2
1 -3 1 2 2
2
8 5
1 1 2 2 3 3 4
-2 -6 1 -7 8 1 1 6
8
Hint
Explanation for Sample 1:
All apples can be picked.
Explanation for Sample 2:
Only the apples at nodes and can be picked. Node cannot be visited because there are too many birds.
Explanation for Sample 3:

At node , Charlie gives away apples. First, he picks all apples at nodes , and now he has apples in his pocket. Then he passes through node , and takes the apples at node . Node is not worth visiting because there are too many birds.
One optimal specific path is: $1 \rightarrow 3 \rightarrow 6 \rightarrow 3 \rightarrow 7 \rightarrow 3 \rightarrow 1 \rightarrow 2 \rightarrow 5 \rightarrow 2 \rightarrow 1$.
Constraints and Notes
This problem uses bundled testdata.
- Subtask 1 (10 pts): .
- Subtask 2 (20 pts): .
- Subtask 3 (10 pts): .
- Subtask 4 (30 pts): .
- Subtask 5 (30 pts): no special restrictions.
For of the testdata, , , , and .
“Do you remember? In front of the door, there were two trees. One was an apple tree, and the other one... was also an apple tree.”
Translated by ChatGPT 5