Airplane | 프로그래밍의 벗 PivotOJ
PivotOJ

Airplane

시간 제한: 1000ms메모리 제한: 1024MB출처: NOI 2023BOJ 28497

문제

Benson the Rabbit wants to fly an airplane!

There are nn regions that Benson can fly in, numbered from 11 to nn. For each region ii, there is a minimum altitude a[i]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 mm such pairs numbered from 11 to mm, and the jj-th pair u[j]u[j] and v[j]v[j] indicates that Benson can fly between regions u[j]u[j] and v[j]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 11 at height 00. He wants to travel to region nn, and to land he must end at height 00.

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 11, decrease by 11 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 nn?

입력

The first line of input will contain 22 spaced integers nn and mm, which represent the number of regions and the number of pairs of regions that Benson can fly between.

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

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

출력

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

예제

예제 1

입력
3 2
0 2 0
1 2
2 3
출력
4

예제 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
출력
5
코드를 제출하려면 로그인하세요.