#Lutece2586. The Number of Procedures

The Number of Procedures

Migrated from Lutece 2586 The Number of Procedures

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 directed tree with the root 1. All the nodes have an initial value aa of DFS order.

After calling dfs(1) the following algorithm returns aa as a DFS order of a tree rooted at 11 :


order := 0
a := array of length n 

function dfs(u):
    order := order + 1
    a[u] := order
    for all v such that there is a directed edge (u -> v):
        dfs(v)

Note that there may be different DFS orders for a given tree.

After that, we will perform some procedures on the tree. One procedure is as follows.

  • Among all directed edges uvu\to v such that au<ava_u<a_v, select the edge uvu'\to v' with the lexicographically smallest pair (au,av)(a_{u'},a_{v'}).
  • Swap aua_{u'} and ava_{v'} .

You will get the final value aa of each node. Your task is to answer how many procedures are performed on the tree.

Input

The first line of the input contains an integer nn — the number of nodes on the tree.

The second line contains n integers a1,a2,,ana_1,a_2,\ldots ,a_n — the current vlaue of the tree.

Each of the next n1n-1 lines contains two integers ui,vi (uivi)u_i,v_i\ (u_i\neq v_i), describing a directed edge from uiu_i to viv_i. The edges form a directed tree rooted at 1.

Output

One integer, the number of procedures.

Samples

7
4 5 2 1 7 6 3
1 5
7 6
1 2
2 7
3 4
1 3
5

Constraints

1n1000001\le n \le 100000 1ai,u,vn1\le a_i,u,v\le n

Note

The picture is changed as the sample run. avatar

Resources

2021 UESTC ICPC Training for Graph