#aising2019e. [aising2019_e]Attack to a Tree

[aising2019_e]Attack to a Tree

Problem Statement

The server in company A has a structure where NN devices numbered 1,2,...,N1, 2, ..., N are connected with N1N - 1 cables. The ii-th cable connects Device UiU_i and Device ViV_i. Any two different devices are connected through some number of cables.

Each device vv (1leqvleqN1 \\leq v \\leq N) has a non-zero integer AvA_v, which represents the following:

  • If Av<0A_v < 0, Device vv is a computer that consumes an electric power of \-Av\-A_v.
  • If Av>0A_v > 0, Device vv is a battery that supplies an electric power of AvA_v.

You have decided to disconnect some number of cables (possibly zero) to disable the server. When some cables are disconnected, the devices will be divided into some number of connected components. The server will be disabled if all of these connected components satisfy one of the following conditions:

  • There is no computer in the connected component. That is, AvA_v is positive for every device vv that belongs to the connected component.
  • There is not enough supply of electric power in the connected component. That is, the sum of AvA_v over all devices vv that belong to the connected component is negative.

At least how many cables do you need to disconnect in order to disable the server?

Constraints

  • 1leqNleq51 \\leq N \\leq 5 000000
  • 1leqAileq1091 \\leq |A_i| \\leq 10^9 (1leqileqN1 \\leq i \\leq N)
  • 1leqUi,VileqN1 \\leq U_i, V_i \\leq N (1leqileqN11 \\leq i \\leq N - 1)
  • UineqViU_i \\neq V_i (1leqileqN11 \\leq i \\leq N - 1)
  • Any two different devices are connected through some number of cables.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN A1A_1 A2A_2 ...... ANA_N U1U_1 V1V_1 U2U_2 V2V_2 :: UN1U_{N - 1} VN1V_{N - 1}

Output

Print the answer.


Sample Input 1

7
-2 7 5 6 -8 3 4
1 2
2 3
2 4
1 5
5 6
5 7

Sample Output 1

1

We should disconnect the cable connecting Device 11 and Device 22.


Sample Input 2

4
1 2 3 4
1 2
1 3
1 4

Sample Output 2

0

Sample Input 3

6
10 -1 10 -1 10 -1
1 2
2 3
3 4
4 5
5 6

Sample Output 3

5

Sample Input 4

8
-2 3 6 -2 -2 -5 3 2
3 4
7 6
6 2
8 2
5 3
1 8
3 7

Sample Output 4

3

Sample Input 5

10
3 4 9 6 1 5 -1 10 -10 -10
7 4
5 6
8 1
9 5
7 1
10 3
2 8
4 10
9 2

Sample Output 5

3