QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100 Hackable ✓

#13618. Triangle

الإحصائيات

Snuke has a rooted tree with $n$ nodes. Each node has a weight $w_i$, and initially, there are no stones on any node.

Snuke has prepared some stones and is holding them in her hand. She can perform the following two operations any number of times:

  1. Place $w_i$ stones on node $i$ from her hand. This operation can only be performed if every child node $j$ of node $i$ already has exactly $w_j$ stones on it.
  2. Remove all stones from node $i$ and return them to her hand.

Takahashi wants to know, for each node $i$, what is the minimum number of stones Snuke needs to prepare in her hand in order to successfully place $w_i$ stones on node $i$ (according to the above rules).

Input Format

Read from standard input.

  • The first line contains a single integer $T$, indicating the subtask ID.
  • The second line contains a single integer $n$ $(n \leq 2 \times 10^5)$.
  • The third line contains $n-1$ positive integers. The $i$-th of these numbers is $p_i$, the parent of node $i+1$. It is guaranteed that $p_i < i+1$.
  • The fourth line contains $n$ positive integers, where the $i$-th number is $w_i$ — the weight of node $i$.

Output Format

Output to standard output.

Print one line with $n$ integers. The $i$-th integer is the answer for node $i$: the minimum number of stones Snuke must prepare in order to place $w_i$ stones on node $i$.

Examples

Input

0
3
1 2
1 1 1

Output

2 2 1

Input

0
3
1 1
1 1 1

Output

3 1 1

Scoring

For all test cases:

  • $n \leq 2 \times 10^5$
  • $1 \le p\_i < i$
  • $1 \le w\_i \le 10^9$
Subtask ID Special Condition Score
1 $n \leq 15$ 9
2 $n \leq 2000$ 19
3 All $w_i$ are the same 6
4 $w_i \leq w_{i+1}$ 12
5 $n \leq 2000$ and all nodes have degree $\leq 2$ 5
6 All nodes except the root have degree $\leq 2$ 13
7 No additional constraints 36
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.