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

#FT0000185. Trò chơi trung vị trên cây (Game on Tree 2)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Trò chơi trung vị trên cây (Game on Tree 2)

Game on Tree 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree has NN vertices, with an even integer AiA_i written on each vertex. A token starts at vertex 11. Taro moves first; Jiro and Taro then alternate. A player must move the token to an adjacent vertex not visited before. The game ends when no move is available. Consider the multiset of values on all visited vertices, including vertex 11. For an odd number KK, the median is the (K+1)/2(K+1)/2-th value in sorted order; for even KK, it is the average of the K/2K/2-th and (K/2+1)(K/2+1)-th values. Taro maximizes and Jiro minimizes the final median. Find the result under optimal play. All values are even, so the median is integral.

Input

The first line contains NN; the second contains A1,…,ANA_1,\ldots,A_N. The next N−1N-1 lines contain undirected edges u v with u<vu<v.

Output

Print one integer: the final median under optimal play.

Subtasks

  • Subtask 1 (20 points): 2≤N≤142\le N\le14; 2≤Ai≤1092\le A_i\le10^9 and every AiA_i is even; exactly N−1N-1 edges u v with 1≤u<v≤N1\le u<v\le N form a tree.
  • Subtask 2 (30 points): 2≤N≤20002\le N\le2000; 2≤Ai≤1092\le A_i\le10^9 and every AiA_i is even; exactly N−1N-1 edges u v with 1≤u<v≤N1\le u<v\le N form a tree.
  • Subtask 3 (50 points): 2≤N≤1000002\le N\le100000; 2≤Ai≤1092\le A_i\le10^9 and every AiA_i is even; exactly N−1N-1 edges u v with 1≤u<v≤N1\le u<v\le N form a tree.

Examples

Example 1

Input

5
2 4 6 8 10
4 5
3 4
1 5
2 4

Output

7

Explanation

At leaf 3, path [1, 5, 4, 3], sorted values [2, 6, 8, 10], median = 7. At leaf 2, path [1, 5, 4, 2], sorted values [2, 4, 8, 10], median = 6. At vertex 4, Taro (max) chooses among child values [7, 6], obtaining 7. At vertex 5, Jiro (min) chooses among child values [7], obtaining 7. At vertex 1, Taro (max) chooses among child values [7], obtaining 7.

Example 2

Input

5
6 4 6 10 8
1 4
1 2
1 5
1 3

Output

8

Explanation

At leaf 4, path [1, 4], sorted values [6, 10], median = 8. At leaf 2, path [1, 2], sorted values [4, 6], median = 5. At leaf 5, path [1, 5], sorted values [6, 8], median = 7. At leaf 3, path [1, 3], sorted values [6, 6], median = 6. At vertex 1, Taro (max) chooses among child values [8, 5, 7, 6], obtaining 8.