Under construction

Still being built. Things may change

Educational Tree Problem

Interview

Time limit1sMemory limit1024 MB

Summary
Given a rooted tree where each child's value is at most its parent's, find the maximum sum of a parent-closed vertex set for each size k from 1 to N.
Level

Medium4 of 10

Topics
Sorting, Tree, Greedy
Solved
No attempts yet

Problem

The tree has NN vertices. Each vertex is numbered from 11 to NN with no duplicates, and vertex 11 is the root of the tree.

For every integer ii with 2≤i≤N2 \leq i \leq N, the parent of vertex ii is vertex pip_i.

Vertex ii has the integer AiA_i written on it, and Ai≤ApiA_i \leq A_{p_i} always holds. Choose some vertices to maximize the sum of the integers written on the chosen vertices. Every chosen vertex must be either vertex 11 or have its parent also chosen.

Find the answer for each possible number of chosen vertices.

Input

The first line gives the integer NN. (2≤N≤300 000)(2 \leq N \leq 300\ 000)

The second line gives the sequence p2,p3,⋯ ,pNp_2, p_3, \cdots, p_N, separated by spaces. (1≤pi<i)(1 \leq p_i < i)

The third line gives the sequence A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces. (1≤Ai≤109)(1 \leq A_i \leq 10^9)

Output

Print NN lines. On the ii-th line, print the answer when exactly ii vertices are chosen.

Examples1

  1. Example 1

    Input
    4
    1 1 2
    4 4 3 1
    
    Expected output
    4
    8
    11
    12