#Lutece1559. B0n0 Path

B0n0 Path

Migrated from Lutece 1559 B0n0 Path

All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.

Description

There is a country with NN cities, there are roads between some pairs of cities.

For every pair of cities, there is exactly one path between them, on which there are no cities passed more than once.

So it is obvious that there are N1N-1 roads and N(N1)2\frac{N(N-1)}{2} paths.

The cities number from 11 to NN, and the ithi^{th} city has one number AiA_i.

A path is Bono if and only if the numbers AiA_i on the path are non-strictly increasing or decreasing.

i.e., if the numbers are Ab1,Ab2,,AbmA_{b_1}, A_{b_2}, \dots, A_{b_m}, AbiAbi+1 ,1i<mA_{b_i}\leq A_{b_{i+1}}\ , \forall 1\leq i\lt m or AbiAbi+1 ,1i<mA_{b_i}\geq A_{b_{i+1}}\ , \forall 1\leq i\lt m should be satisfied.

How many Bono paths in the country?

Input

The first line contains one integer NN.

The second line contains NN integers, where the ithi^{th} indicates AiA_i.

For the next N1N-1 lines, each line contains two integers u,vu, v, meaning that there is a road between uthu^{th} city and vthv^{th} city.

$1\leq N\leq 10^5, 1\leq u, v\leq N, 1\leq A_i \leq 10^9$

Output

One integer indicating the number of bono paths in the country.

Samples

4
1 7 1 9
1 3
1 4
2 1
5
6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6
15

Note

For sample 1:

sample1

The format of the text on ithi^{th} node is (i:Ai)(i:A_i).

There are 5 bono paths: (1,2),(1,3),(1,4),(2,1,3),(3,1,4)(1, 2), (1, 3), (1, 4), (2, 1, 3), (3, 1, 4), while path (2,1,4)(2, 1, 4) is not bono path.

For sample 2:

title

All path are bono paths.

Resources

The 15th UESTC Programming Contest Preliminary