Recently, Eva learned several advanced data structure techniques for trees. To show her progress, she asked Colin to give her a challenging problem about tree statistics.
However, Colin just gave her a very typical problem:
Given a tree with
vertices, and the vertex
is the root. Each vertex has an integer weight
.
Let
represent the lowest common ancestor of the vertex
and
.
Given two integer sequences
and
.
Define the joint power of these two sequences to be
Please calculate the value of this joint power.
Eva thinks this problem is too easy, so she decided to change the problem into an online problem:
First, given the tree and the weight of each vertex. Initially, both sequences are empty.
Then there will be

operations, each one is of the following two types:
-
1 u : append
to the end of the first sequence.
-
2 u : append
to the end of the second sequence.
After each operation, you need to output the current joint power of the two sequences.
However, this version of the problem is too hard for Eva, so she added an additional limitation that, after each operation, the sum of values in the first sequence will not exceed

.
Now Eva turns this problem to you, do you know how to solve it?