Cat Exercise

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There are NN cat towers, numbered from 11 to NN. The height of Tower ii (1iN1 ≤ i ≤ N) is P_iP\_i. The heights of the towers are distinct integers between 11 and NN, inclusive. There are N1N - 1 adjacent pairs of towers. For each jj (1jN11 ≤ j ≤ N - 1), Tower A_jA\_j and Tower B_jB\_j are adjacent to each other. In the beginning, it is possible to travel from a tower to any other tower by repeating moves from towers to adjacent towers.

In the beginning, a cat stays in a tower of height NN.

Then we perform cat exercises. In cat exercises, we repeatedly choose a tower and put an obstacle on it. However, we cannot put an obstacle on a tower where we already put an obstacle on it. During the process, the following will happen.

  • If the cat does not stay in the chosen tower, nothing will happen.
  • If the cat stays in the chosen tower and there is an obstacle on every tower which is adjacent to the chosen tower, the cat exercises will finish.
  • Otherwise, among the towers where the cat can arrive by repeating moves from towers to adjacent towers without obstacles, the cat will move to the highest tower except for the current tower by repeating moves from towers to adjacent towers. In this process, the cat takes the route where the number of moves from towers to adjacent towers becomes minimum.

Given information of the heights of the towers and pairs of adjacent towers, write a program which calculates the maximum possible sum of the number of moves of the cat from towers to adjacent towers if we put obstacles suitably

입력

Read the following data from the standard input.

NN

P_1P\_1 P_2P\_2 \cdots P_NP\_N

A_1A\_1 B_1B\_1

A_2A\_2 B_2B\_2

\vdots

A_N1A\_{N-1} B_N1B\_{N-1}

출력

Write one line to the standard output. The output should contain the maximum possible sum of the number of moves of the cat from towers to adjacent towers.

제한

  • 2N200,0002 ≤ N ≤ 200\\,000.
  • 1P_iN1 ≤ P\_i ≤ N (1iN1 ≤ i ≤ N).
  • P_iP_jP\_i \ne P\_j (1i<jN1 ≤ i < j ≤ N).
  • 1A_j<B_jN1 ≤ A\_j < B\_j ≤ N (1jN11 ≤ j ≤ N - 1).
  • In the beginning, it is possible to travel from a tower to any other tower by repeating moves from towers to adjacent towers.
  • Given values are all integers.