#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 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 roads and paths.
The cities number from to , and the city has one number .
A path is Bono if and only if the numbers on the path are non-strictly increasing or decreasing.
i.e., if the numbers are , or should be satisfied.
How many Bono paths in the country?
Input
The first line contains one integer .
The second line contains integers, where the indicates .
For the next lines, each line contains two integers , meaning that there is a road between city and 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:
The format of the text on node is .
There are 5 bono paths: , while path is not bono path.
For sample 2:
All path are bono paths.
Resources
The 15th UESTC Programming Contest Preliminary