There is a rooted tree with

vertices and the root vertex is

. In each vertex, there is a monster. The hit points of the monster in the

-th vertex is

.
Kotori would like to kill all the monsters. The monster in the

-th vertex could be killed if the monster in the direct parent of the

-th vertex has been killed. The power needed to kill the

-th monster is the sum of

and the hit points of all other living monsters who lives in a vertex

whose direct parent is

. Formally, the power equals to
In addition, Kotori can use some magic spells. If she uses one magic spell, she can kill any monster using

power without any restriction. That is, she can choose a monster even if the monster in the direct parent is alive.
For each

, Kotori would like to know, respectively, the minimum total power needed to kill all the monsters if she can use

magic spells.