Under construction

Still being built. Things may change

Secret Base

Time limit7sMemory limit1024 MB

Summary
Given a weighted tree where point weights change, after each update find the minimum total weighted distance from all people to a single city.
Level

Medium7 of 10

Topics
Tree, Segment tree, Binary search
Solved
No attempts yet

Statement

설곽국 is a country with N cities numbered from 1 to NN. There are N−1N-1 roads connecting pairs of cities, and all cities are connected. Every road has length 11.

One day, the neighboring country 경곽국 announced an invasion through a news report. The nervous people of 설곽국 decided to build a secret base. The base is built temporarily and simply, so it can be moved easily.

The base is built in a city that satisfies the following property.

  • If the base is built in city xx, define f(x)=∑i=1nAi×dist(i,x)f(x)=\sum_{i=1}^n A_i \times \text{dist}(i, x) as the sum of the travel distances of all people to city xx. Here dist(x,y)\text{dist}(x, y) is the distance between city xx and city yy.
  • The base is built in a city xx where f(x)f(x) is minimal among all cities. If several cities qualify, the base can be built in any of them.

Brue, who lives in 설곽국, wants to know the total travel distance of the people after building the base.

Over time, the number of people in a city changes exactly QQ times. After each change, the total travel distance may change, and the base may move as a result. Find the total travel distance for every time point.

Input

The first line contains the number of cities NN and the number of changes QQ.

The second line contains the number of people in each city A1A_1, A2A_2, ⋯\cdots, ANA_N, separated by spaces.

From the third line, the next N−1N-1 lines describe the road structure of 설곽국. The ii-th line contains two integers xix_i and yiy_i, meaning that road ii connects city xix_i and city yiy_i.

From line N+2N+2, the next QQ lines describe the changes as viv_i did_i. This means the number of people in city viv_i becomes did_i.

Output

Print Q+1Q+1 lines. The first line is the total travel distance to the base in the initial state, and each of the next QQ lines is the total travel distance after each population change.

Constraints

  • 2≤N≤2×1052 \le N \le 2 \times 10^5
  • 2≤Q≤2×1052 \le Q \le 2 \times 10^5
  • 1≤Ai≤1061 \le A_i \le 10^6 (1≤i≤N1 \le i \le N)
  • 1≤xi≤N1 \le x_i \le N
  • 1≤yi≤N1 \le y_i \le N
  • The input is guaranteed to be a valid tree.
  • 1≤vi≤N1 \le v_i \le N (1≤i≤Q1 \le i \le Q)
  • 1≤di≤1061 \le d_i \le 10^6 (1≤i≤Q1 \le i \le Q)

Hint

The distance between two cities aa and bb is the number of roads on the shortest path from city aa to city bb.

Examples1

  1. Example 1

    Input
    4 3
    1 1 1 1
    1 2
    2 3
    3 4
    2 2
    3 2
    4 100
    
    Expected output
    4
    4
    5
    9