아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Airplane

시간 제한1초메모리 제한1024 MB

요약
각 지역의 최소 고도를 지키며 지역 1에서 출발해 지역 n에 고도 0으로 도착하는 최소 시간을 구한다.
난이도

보통10점 중 7점

유형
최단 경로, 그래프, 이분 탐색, 그리디
정답자
아직 제출이 없습니다

문제

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≤n≤200,0001 ≤ n ≤ 200\\,000
  • 1≤m≤400,0001 ≤ m ≤ 400\\,000
  • 0≤a\[i]≤1080 ≤ a\[i] ≤ 10^8
  • a\[1]=a\[n]=0 a\[1] = a\[n] = 0
  • 1≤u\[j],v\[j]≤n1 ≤ u\[j], v\[j] ≤ n, u\[j]≠v\[j]u\[j] \ne v\[j]

예제2

  1. 예제 1

    입력
    3 2
    0 2 0
    1 2
    2 3
    
    예상 출력
    4
    
  2. 예제 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