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

문제

An undirected graph with $N$ vertices is given. Each vertex $i$ has an integer value $A_i$. The graph has $M$ edges, and all edges have length $1$.

You want to choose two distinct vertices $a$ and $b$ that satisfy the following condition:

  • There exists at least one path of length exactly $2$ between vertices $a$ and $b$. Note that if $a$ and $b$ are directly connected by an edge (i.e., their shortest distance is $1$), the condition is satisfied as long as there exists at least one path of length $2$ between them.
  • The definition of a path is as follows: A path is a sequence of vertices such that every two consecutive vertices are connected by an edge. The length of a path is the number of edges it uses. A path may contain the same vertex multiple times.

Among all pairs of vertices $(a, b)$ that satisfy the condition, find the minimum possible value of $|A_a + A_b|$. In other words, choose two distinct vertices that share a common neighbor so that the absolute value of the sum of their weights is as small as possible.

입력

The first line contains two integers $N$ and $M$, the number of vertices and the number of edges. ($3 \le N \le 100\,000, 2 \le M \le 2N$)

The second line contains $N$ integers $A_1, A_2, \ldots, A_N$, where $A_i$ is the value of vertex $i$. ($-10^9 \le A_i \le 10^9$)

Each of the next $M$ lines contains two integers $u$ and $v$, indicating an undirected edge between vertices $u$ and $v$. ($1 \le u < v \le N$)

It is guaranteed that the given graph is connected and contains no multiple edges or self-loops.

출력

Print a single integer: the minimum value of $|A_a + A_b|$ among all pairs of distinct vertices $(a, b)$ such that there exists at least one path of length exactly $2$ between $a$ and $b$. It is guaranteed that at least one such pair $(a, b)$ exists.

서브태스크

번호배점제한
140

$3 \le N \le 100$

220

$3 \le N \le 2\,000$

320

$3 \le N \le 100\,000$, In this subtask, all $A_i$ satisfies $A_i \ge 0$

420

$3 \le N \le 100\,000$

예제 입력 1

3 2
4 3 5
1 3
2 3

예제 출력 1

7

There is a path of length $2$ between vertices $(1, 2)$, namely 1-3-2. On the other hand, there is no path of length $2$ between $(1, 3)$ or $(2, 3)$, so the only valid pair $(a, b)$ is $(1, 2)$. In this case, $|A_a + A_b| = 7$.

This example satisfies the conditions of all subtasks.

예제 입력 2

3 3
-4 -3 5
1 2
1 3
2 3

예제 출력 2

1

In this example, all pairs $(1, 2)$, $(1, 3)$, and $(2, 3)$ have a path of length $2$: 1-3-2, 1-2-3, and 2-1-3, respectively. In this case, the pair $(a, b)$ that minimizes the absolute value of the sum of their weights is $(1, 3)$, and the minimum value is $1$.

This example satisfies the conditions of Subtask 1, 2, and 4

예제 입력 3

4 4
-3 2 -4 7
1 2
2 3
3 4
1 4

예제 출력 3

7

In this example, only the two pairs $(1, 3)$ and $(2, 4)$ have a path of length $2$ between them.

This example satisfies the conditions of Subtask 1, 2, and 4.

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 2이다.