Lemon Tree

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 2.0s
Memory limit: 1G

Author:
Problem types

You have a lemon tree containing N lemons connected with N-1 branches. Each lemon has a value. We call the floor of the arithmetic mean of the values on the path from u to v the relation between lemon u and lemon v. Tommy can only eat the two lemons if their relation is not greater than M; otherwise, Tommy will have syncope. Please help Tommy determine how many pairs of lemons he can eat.

Input Specification

The first line contains two integers, N (3 \le N \le 10^5) and M (1 \le M \le 10^3).

The next line contains N integers. The i^\text{th} integer, a_i (-10^3 \le a_i \le 10^3), represents the value of i^\text{th} lemon.

The next N-1 lines will each contain two integers, u_i and v_i (1 \le u_i, v_i \le N), indicating that there is a branch between lemon u_i and v_i.

Output Specification

Output the number of pairs of lemons Tommy can eat.

Constraints

Subtask Points Additional constraints
1 40 3 \le N \le 10^3
2 60 No additional constraints.

Sample Input 1

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

Sample Output 1

2

Explanation for Sample 1

The two pairs of lemons Tommy can eat are (3, 6) and (4, 6).

Sample Input 2

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

Sample Output 2

2

Comments

There are no comments at the moment.