2023-ho-t4_Cat_Exercise (JOI JOI)
Time limit: 2 seconds | Memory limit: 1024 MB | Task type: batch
Algorithms: greedy, dynamic programming, union-find, data structures, tree, lowest common ancestor

![](_page_0_Picture_0.jpeg)

# 4 Cat Exercise

There are *N* cat towers, numbered from 1 to *N*. The height of Tower *i* (1 ≤ *i* ≤ *N*) is *Pi*. The heights of the towers are distinct integers between 1 and *N*, inclusive. There are *N* − 1 adjacent pairs of towers. For each *j* (1 ≤ *j* ≤ *N* − 1), Tower *Aj* and Tower *Bj* 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 *N*.

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.

### Input

Read the following data from the standard input.

```
N
P1 P2 ... PN
A1 B1
A2 B2
...
AN-1 BN-1
```
![](_page_1_Picture_0.jpeg)

## Output

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.

## Constraints

- 2 ≤ *N* ≤ 200 000.
- 1 ≤ *Pi* ≤ *N* (1 ≤ *i* ≤ *N*).
- *Pi* ≠ *Pj* (1 ≤ *i* < *j* ≤ *N*).
- 1 ≤ *Aj* < *Bj* ≤ *N* (1 ≤ *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.

### Subtasks

1. (7 points) *Ai* = *i*, *Bi* = *i* + 1 (1 ≤ *i* ≤ *N* − 1), *N* ≤ 16.
2. (7 points) *Ai* = *i*, *Bi* = *i* + 1 (1 ≤ *i* ≤ *N* − 1), *N* ≤ 300.
3. (7 points) *Ai* = *i*, *Bi* = *i* + 1 (1 ≤ *i* ≤ *N* − 1), *N* ≤ 5 000.
4. (10 points) *N* ≤ 5 000.
5. (20 points) *Ai* = *i*, *Bi* = *i* + 1 (1 ≤ *i* ≤ *N* − 1).
6. (23 points) *Ai* = ⌊(i+1)/2⌋, *Bi* = *i* + 1 (1 ≤ *i* ≤ *N* − 1). Here ⌊*x*⌋ is the largest integer which is smaller than or equal to *x*.
7. (26 points) No additional constraints.

![](_page_2_Picture_0.jpeg)

### Sample Input and Output

| Sample Input 1 | Sample Output 1 |
|----------------|-----------------|
| 4              | 3               |
| 3 4 1 2        |                 |
| 1 2            |                 |
| 2 3            |                 |
| 3 4            |                 |

If we perform the cat exercises in the following way, the cat moves 3 times in total.

- We put an obstacle on Tower 1. The cat does not move.
- We put an obstacle on Tower 2. The cat moves from Tower 2 to Tower 3. Then, the cat moves from Tower 3 to Tower 4.
- We put an obstacle on Tower 4. The cat moves from Tower 4 to Tower 3.
- We put an obstacle on Tower 3. Then the cat exercises finish.

Since there is no way to perform cat exercises where the cat moves more than or equal to 4 times from towers to adjacent towers, output 3.

This sample input satisfies the constraints of Subtasks 1, 2, 3, 4, 5, 7.

| Sample Input 2 | Sample Output 2 |
|----------------|-----------------|
| 7              | 7               |
| 3 2 7 1 5 4 6  |                 |
| 1 2            |                 |
| 1 3            |                 |
| 2 4            |                 |
| 2 5            |                 |
| 3 6            |                 |
| 3 7            |                 |

This sample input satisfies the constraints of Subtasks 4, 6, 7.

Starter code:

Grader code:

Setup script:

Evaluation script: