atcoder#ARC115F. [ARC115F] Migration

[ARC115F] Migration

Score : 10001000 points

Problem Statement

Given is a tree with NN vertices numbered 1,,N1, \ldots, N. The ii-th edge connects Vertex uiu_i and Vertex viv_i. Additionally, Vertex ii has an integer hih_i written on it. We have KK pieces, the ii-th of which is initially on Vertex sis_i. You will repeat the following operation: choose a piece and move it to one of the vertices adjacent to the vertex the piece is currently on. You will end this process when each piece ii is on Vertex tit_i. It is not required for each piece ii to go along the shortest path from Vertex sis_i to Vertex tit_i. For an arrangement of the pieces, let its potential be the sum of the integers written on the vertices the pieces are on. If multiple pieces are on the same vertex, the integer on that vertex is added the number of times equal to that number of pieces. Find the minimum possible value of the maximum potential during the process, including the initial and final states.

Constraints

  • 1N,K20001 \leq N,K \leq 2000
  • 1ui,viN1 \leq u_i,v_i \leq N
  • 1hi1091 \leq h_i \leq 10^9
  • 1si,tiN1 \leq s_i,t_i \leq N
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

NN

h1h_1 h2h_2 \ldots hNh_N

u1u_1 v1v_1

u2u_2 v2v_2

::

uN1u_{N-1} vN1v_{N-1}

KK

s1s_1 t1t_1

s2s_2 t2t_2

::

sKs_K tKt_K

Output

Print the answer.

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

We can make 44 the maximum potential during the process, as follows:

  • Initially, the potential is 33.
  • Move Piece 22 to Vertex 22. The potential is now 44.
  • Move Piece 22 to Vertex 11. The potential is now 22.
  • Move Piece 11 to Vertex 22. The potential is now 44.
  • Move Piece 11 to Vertex 33. The potential is now 33.

There is no way to make the maximum potential during the process less than 44, so the answer is 44.

7
100 101 1 100 101 1 1000
1 2
2 3
4 5
5 6
1 7
4 7
2
1 3
4 6
201
5
2 1 100 5 6
1 2
2 3
3 4
3 5
2
2 2
4 5
101
4
1 2 3 100
1 4
2 4
3 4
9
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
115
6
1 100 1 1 10 1000
1 2
2 3
4 5
1 6
4 6
3
1 3
5 5
5 5
102