PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000296. Tổng cây con tĩnh (Static Subtree Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng cây con tĩnh (Static Subtree Sum)

Static Subtree Sum

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree with NN vertices numbered 11 through NN is rooted at vertex 1. Vertex vv has a fixed integer weight ava_v. For every query vertex vv, sum the weights of all its descendants including itself. No weights change.

The subtree rooted at vv includes vv and every vertex whose path from root 1 passes through vv.

Input

  • The first line contains two integers N,QN,Q, the number of vertices and the number of queries.
  • The second line contains exactly NN integers a1,…,aNa_1,\ldots,a_N, the weights in vertex order.
  • Each of the next N−1N-1 lines contains two integers x,yx,y, the endpoints of an edge.
  • Each of the final QQ lines contains one integer vv, the root of the subtree whose sum is requested.

Output

Print QQ integers, one per line, in query order.

Subtasks

  • Subtask 1 (20 points): 1≤N,Q≤2000001\le N,Q\le200000, ∣av∣≤109|a_v|\le10^9, 1≤x,y≤N1\le x,y\le N, 1≤v≤N1\le v\le N. The edges form an undirected tree; all data are integers. Additionally: N≤30,Q≤40N\le30, Q\le40.
  • Subtask 2 (30 points): 1≤N,Q≤2000001\le N,Q\le200000, ∣av∣≤109|a_v|\le10^9, 1≤x,y≤N1\le x,y\le N, 1≤v≤N1\le v\le N. The edges form an undirected tree; all data are integers. Additionally: the tree is a simple path (each vertex has degree at most 2).
  • Subtask 3 (50 points): 1≤N,Q≤2000001\le N,Q\le200000, ∣av∣≤109|a_v|\le10^9, 1≤x,y≤N1\le x,y\le N, 1≤v≤N1\le v\le N. The edges form an undirected tree; all data are integers.

Example 1

Input

5 4
3 -2 4 7 -1
1 2
1 3
2 4
2 5
1
2
3
5

Output

11
4
4
-1

Explanation

For query 1 the entire tree sums to 3−2+4+7−1=113-2+4+7-1=11. The subtree of 2 contains vertices 2, 4, 5, giving −2+7−1=4-2+7-1=4. Vertices 3 and 5 are leaves, so the last two answers are 4 and −1-1.

Example 2

Input

1 2
-9
1
1

Output

-9
-9

Explanation

Both queries refer to the subtree of vertex 1, consisting of a single vertex of weight −9-9. Print −9-9 twice.