Logo
(追記) (追記ここまで)

27539번 - Cat Exercise 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB2251139648.731%

문제

There are $N$ cat towers, numbered from 1ドル$ to $N$. The height of Tower $i$ (1ドル ≤ i ≤ N$) is $P_i$. 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 $A_j$ and Tower $B_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 $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

입력

Read the following data from the standard input.

$N$

$P_1$ $P_2$ $\cdots$ $P_N$

$A_1$ $B_1$

$A_2$ $B_2$

$\vdots$

$A_{N-1}$ $B_{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.

제한

  • 2ドル ≤ N ≤ 200,000円$.
  • 1ドル ≤ P_i ≤ N$ (1ドル ≤ i ≤ N$).
  • $P_i \ne P_j$ (1ドル ≤ i < j ≤ N$).
  • 1ドル ≤ A_j < B_j ≤ 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.

서브태스크

번호배점제한
17

$A_i = i,ドル $B_i = i + 1$ (1ドル ≤ i ≤ N - 1$), $N ≤ 16$.

27

$A_i = i,ドル $B_i = i + 1$ (1ドル ≤ i ≤ N - 1$), $N ≤ 300$.

37

$A_i = i,ドル $B_i = i + 1$ (1ドル ≤ i ≤ N - 1$), $N ≤ 5,000円$.

410

$N ≤ 5,000円$.

520

$A_i = i,ドル $B_i = i + 1$ (1ドル ≤ i ≤ N - 1$).

623

$A_i = \left\lfloor\frac{i+1}{2}\right\rfloor,ドル $B_i = i + 1$ (1ドル ≤ i ≤ N - 1$). Here $\left\lfloor x \right\rfloor$ is the largest integer which is smaller than or equal to $x$.

726

No additional constraints.

예제 입력 1

4
3 4 1 2
1 2
2 3
3 4

예제 출력 1

3

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.

예제 입력 2

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

예제 출력 2

7

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

힌트

출처

Olympiad > Japanese Olympiad in Informatics > JOI 2022/2023 4번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /