Under construction

Still being built. Things may change

Defense Tower

Time limit6sMemory limit512 MB

Summary
Given a tree of cities with tower powers, each upgrade makes a tower the newest, and the task is to print the sum of protector labels after every upgrade.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Segment tree
Solved
No attempts yet

Problem

In ICPCCamp, there are nn cities labeled 1,2,…,n1, 2, \dots, n, connected by (n−1)(n - 1) bidirectional roads. There is exactly one path between any two different cities.

Each city ii has a defense tower with power aia_i. The towers were built in the order n,(n−1),…,1n, (n - 1), \dots, 1, and each tower has the same number as its city. So tower nn is the oldest and tower 11 is the newest. The effect of tower ii on city jj is eff(i,j)=ai−δ(i,j)\mathit{eff}(i, j) = a_i - \delta(i, j), where δ(i,j)\delta(i, j) is the number of roads between cities ii and jj. The protector of city jj is the tower with the largest effect on jj. If several towers tie on a city, the oldest one is its protector.

Yuuka issues qq commands that upgrade defense towers. The kk-th command adds dkd_k to the power of tower wkw_k. After each command, she wants the sum of the protectors' labels over all cities. The upgraded tower becomes the newest tower.

Upgrading is costly. If the tower being upgraded is not the protector of its own city, or if dk=0d_k = 0, the command is ignored.

Input

The input has zero or more test cases and ends at end-of-file. Each test case is given as follows.

The first line contains two integers nn and qq (1≤n,q≤1051 \leq n, q \leq 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \leq a_i \leq 10^9).

Each of the next n−1n - 1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), meaning there is a road between cities uiu_i and viv_i. There is exactly one path between any two different cities.

Each of the last qq lines contains two integers wkw_k and dkd_k (1≤wk≤n1 \leq w_k \leq n, 0≤dk≤1090 \leq d_k \leq 10^9).

The sum of all nn and the sum of all qq over the test cases do not exceed 10510^5 each.

Output

For each test case, output qq integers s1,s2,…,sqs_1, s_2, \dots, s_q, where sks_k is the sum of the protectors' labels after the kk-th command.

Examples1

  1. Example 1

    Input
    3 3
    1 1 0
    1 3
    2 3
    1 2
    2 2
    3 1000000000
    4 2
    2 4 4 4
    4 1
    4 2
    3 1
    2 4
    2 3
    
    Expected output
    4
    4
    4
    8
    8