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

#LCA0000065. Giá trị lớn thứ k trong cây con (Subtree K-th Max)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Giá trị lớn thứ k trong cây con (Subtree K-th Max)

Subtree K-th Max

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

In a tree rooted at 11, vertex vv has integer value xvx_v. For each query (v,k)(v,k), take the values of all vertices in the subtree of vv, sort them in non-increasing order, and return the kk-th value (1-based). Equal values remain as separate occurrences. Every queried subtree contains at least kk vertices.

Input

First line: n qn\ q. Second line: nn values x1,…,xnx_1,\ldots,x_n. Then n−1n-1 edges u vu\ v and qq lines v kv\ k.

Output

Print the requested value on its own line for each query.

Subtasks

General constraints: 2≤n≤1052\le n\le10^5; 1≤q≤1051\le q\le10^5; 0≤xv≤1090\le x_v\le10^9; 1≤k≤201\le k\le20; kk không vượt kích thước cây con.

  • Subtask 1 (20 points): 1≤n≤401\le n\le 40; 1≤q≤501\le q\le 50.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000.

Examples

Input

5 2
1 2 3 4 5
1 4
2 1
2 5
3 2
1 2
2 1

Output

4
5

Explanation

Subtree 11 has values 1,2,3,4,51,2,3,4,5, whose second-largest value is 44. Subtree 22 has 2,3,52,3,5, whose largest value is 55.