This page is still under construction.

Parts of this page are still being built. What you see may change.

Party joke sets

Time limit1sMemory limit32 MB

Summary
Count distinct joke-type sets from root-connected guest groups with unique values where each subtree below a guest forms consecutive numbers.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, Intervals
Solved
No attempts yet

Problem

Petar is throwing a birthday party and wants to invite some of the employees of the company he runs. Petar is the CEO, and since the party is his, he always attends.

The NN employees, Petar included, are numbered 1 to NN, and person ii tells jokes of type ViV_i. Every employee except Petar has exactly one direct supervisor.

Petar is the CEO, so he has number 1 and is the direct or indirect supervisor of every employee.

Everyone at the party, Petar included, must follow these rules.

  • No two people at the party tell the same type of jokes.
  • Person XX cannot be invited if the direct supervisor of XX is not invited.
  • Person XX cannot be invited if the joke types told by XX together with the joke types told by the invited people that XX supervises, directly or indirectly, do not form a set of consecutive numbers.

A set is a set of consecutive numbers when, after sorting it in ascending order, the difference between adjacent elements is exactly 1. For example, {3,1,2}\{3, 1, 2\} and {5,1,2,4,3}\{5, 1, 2, 4, 3\} are such sets.

Petar wants to know how many different sets of joke types he can see at his party under these rules.

Input

The first line contains the integer NN. (1≤N≤100001 \le N \le 10000)

The second line contains NN integers V1,V2,…,VNV_1, V_2, \dots, V_N, where ViV_i is the type of jokes person ii tells. (1≤Vi≤1001 \le V_i \le 100)

Each of the following N−1N-1 lines contains two integers AA and BB, meaning that person AA is the direct supervisor of person BB. (1≤A,B≤N1 \le A, B \le N)

Output

Print the number of different sets of joke types that comply with the rules.

Note

In the first example the party can show these sets of jokes: {2}\{2\}, {2,3}\{2, 3\}, {2,3,4}\{2, 3, 4\}, {1,2,3,4}\{1, 2, 3, 4\}, {1,2}\{1, 2\}, {1,2,3}\{1, 2, 3\}.

In the second example the only possible sets are {3}\{3\}, {3,4}\{3, 4\}, {3,4,5}\{3, 4, 5\}. The person telling joke 6 cannot come to the party, because then the set of jokes {4,6}\{4, 6\} would not be a set of consecutive numbers.

Examples3

  1. Example 1

    Input
    4
    2 1 3 4
    1 2
    1 3
    3 4
    
    Expected output
    6
    
  2. Example 2

    Input
    4
    3 4 5 6
    1 2
    1 3
    2 4
    
    Expected output
    3
    
  3. Example 3

    Input
    6
    5 3 6 4 2 1
    1 2
    1 3
    1 4
    2 5
    5 6
    
    Expected output
    10