This page is still under construction.

Parts of this page are still being built. What you see may change.

Monster Hunter

Time limit1sMemory limit512 MB

Summary
On a rooted tree, killing a vertex costs its hp plus the hp of its living children; with up to m free kills by spell, find the minimum total power for every m from 0 to n.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Greedy, DFS
Solved
No attempts yet

Problem

There is a rooted tree with nn vertices, and the root vertex is 11. Each vertex holds a monster. The hit points of the monster in vertex ii are hp_ihp\_i.

Kotori wants to kill all the monsters. The monster in vertex ii can be killed only if the monster in the direct parent of vertex ii has already been killed. The power needed to kill the ii-th monster is the sum of hp_ihp\_i and the hit points of all other living monsters that live in a vertex jj whose direct parent is ii. Formally, the power equals hp_i+∑_the monster in vertex j is aliveand i is the direct parent of jhp_jhp\_i + \sum\_{\begin{array}{c}\text{the monster in vertex } j \text{ is alive} \\ \text{and } i \text{ is the direct parent of } j \end{array}} hp\_j

Kotori can also use magic spells. If she uses one magic spell, she can kill any monster using 00 power without any restriction. That is, she can choose a monster even if the monster in its direct parent is alive.

For each m=0,1,2,⋯ ,nm=0,1,2,\cdots,n, find the minimum total power needed to kill all the monsters if she can use mm magic spells.

Input

The input consists of multiple test cases. The first line of input contains an integer TT, the number of test cases. Each test case is as follows.

The first line contains an integer nn (2≤n≤2×1032 \le n \le 2 \times 10^3), the number of vertices.

The second line contains (n−1)(n-1) integers p_2,p_3,⋯ ,p_np\_2,p\_3,\cdots,p\_n (1≤p_i<i1 \le p\_i < i), where p_ip\_i is the direct parent of vertex ii.

The third line contains nn integers hp_1,hp_2,⋯ ,hp_nhp\_1,hp\_2,\cdots,hp\_n (1≤hp_i≤1091 \le hp\_i \le 10^9), the hit points of each monster.

The sum of nn over all test cases does not exceed 2×1032 \times 10^3.

Output

For each test case, output one line containing (n+1)(n+1) integers a_0,a_1,⋯ ,a_na\_0, a\_1, \cdots, a\_n separated by spaces, where a_ma\_m is the minimum total power needed to kill all the monsters if Kotori can use mm magic spells.

Examples1

  1. Example 1

    Input
    3
    5
    1 2 3 4
    1 2 3 4 5
    9
    1 2 3 4 3 4 6 6
    8 4 9 4 4 5 2 4 1
    12
    1 2 2 4 5 3 4 3 8 10 11
    9 1 3 5 10 10 7 3 7 9 4 9
    
    Expected output
    29 16 9 4 1 0
    74 47 35 25 15 11 7 3 1 0
    145 115 93 73 55 42 32 22 14 8 4 1 0