시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB112343245.714%

문제

Benson the Rabbit wants to fly an airplane!

There are $n$ regions that Benson can fly in, numbered from $1$ to $n$. For each region $i$, there is a minimum altitude $a[i]$ that Benson must fly at within the region due to terrain constraints.

Additionally, Benson can only fly between certain pairs of regions due to prevailing wind conditions and Benson’s lack of flying experience (he is a rabbit after all). There are $m$ such pairs numbered from $1$ to $m$, and the $j$-th pair $u[j]$ and $v[j]$ indicates that Benson can fly between regions $u[j]$ and $v[j]$ in both directions. It is always possible to travel from any region to all other regions using only the allowed pairs.

Initially, Benson is at region $1$ at height $0$. He wants to travel to region $n$, and to land he must end at height $0$.

In a minute, Benson can choose to stay at his current region or travel to another region. In that same minute, his altitude can increase by $1$, decrease by $1$ or remain the same. However, when Benson arrives at a region, his height must be at least the minimum altitude required for that region. What is the minimum time Benson needs to land at region $n$?

입력

The first line of input will contain $2$ spaced integers $n$ and $m$, which represent the number of regions and the number of pairs of regions that Benson can fly between.

The next line contains $n$ spaced integers $a[1], a[2], \dots , a[n]$, representing the minimum required altitude at each region.

The next $m$ lines of input will contain $2$ spaced integers each. The $j$-th of these lines contains $u[j]$ and $v[j]$, indicating that Benson can fly between regions $u[j]$ and $v[j]$ in both directions.

출력

The output should contain one integer, the minimum time required to land at region $n$.

제한

  • $1 ≤ n ≤ 200\,000$
  • $1 ≤ m ≤ 400\,000$
  • $0 ≤ a[i] ≤ 10^8$
  • $ a[1] = a[n] = 0$
  • $1 ≤ u[j], v[j] ≤ n$, $u[j] \ne v[j]$

서브태스크

번호배점제한
122

$m = n - 1$, $u[j] = j$, $v[j] = j + 1$

210

$n ≤ 2000$, $m ≤ 4000$, $a[i] ≤ 2000$

331

$n ≤ 2000$, $m ≤ 4000$

437

No additional restrictions

예제 입력 1

3 2
0 2 0
1 2
2 3

예제 출력 1

4

Benson is travelling from region $1$ to region $3$. He can do so in $4$ minutes.

  • In minute $1$, Benson stays at region $1$. He increases his altitude from $0$ to $1$.
  • In minute $2$, Benson travels from region $1$ to $2$. He increases his altitude from $1$ to $2$.
  • In minute $3$, Benson travels from region $2$ to $3$. He decreases his altitude from $2$ to $1$.
  • In minute $4$, Benson stays at region $3$. He decreases his altitude from $1$ to $0$.

예제 입력 2

11 12
0 0 0 0 0 0 2 2 1 5 0
1 2
2 3
3 4
4 5
5 6
6 11
1 7
7 8
8 9
9 11
1 10
10 11

예제 출력 2

5

Benson is travelling from region $1$ to region $11$. He can do so in $5$ minutes.

  • In minute $1$, Benson stays at region $1$. He increases his altitude from $0$ to $1$.
  • In minute $2$, Benson travels from region $1$ to $7$. He increases his altitude from $1$ to $2$.
  • In minute $3$, Benson travels from region $7$ to $8$. He maintains his altitude at $2$
  • In minute $4$, Benson travels from region $8$ to $9$. He decreases his altitude from $2$ to $1$.
  • In minute $5$, Benson travels from region $9$ to $11$. He decreases his altitude from $1$ to $0$.

채점 및 기타 정보

  • 예제는 채점하지 않는다.