#abc237e. E - Skiing

E - Skiing

Score : 500500 points

问题描述

在 AtCoder 滑雪场有 NN 个开放区域,分别称为 Space 11、Space 22\ldots、Space NN。其中,Space ii 的海拔高度为 HiH_i。存在 MM 条双向连接两个空间的滑雪道。第 ii 条滑雪道(1iM1 \leq i \leq M)连接了 Space UiU_i 和 Space ViV_i。通过一些滑雪道,可以在任意两个空间之间穿梭。

高桥只能通过滑雪道在各空间之间移动。每次他经过一条滑雪道时,他的幸福感会发生变化。具体来说,当他通过直接相连的滑雪道从 Space XX 移动到 Space YY 时,其幸福感的变化如下:

  • 如果 Space XX 的海拔高于 Space YY,幸福感会增加两者之差:HXHYH_X - H_Y
  • 如果 Space XX 的海拔低于 Space YY,幸福感会减少两者之差的两倍:2(HYHX)2(H_Y - H_X)
  • 如果 Space XX 的海拔等于 Space YY,幸福感不变。

幸福感可能为负值。

最初,高桥位于 Space 11,且其幸福感为 00。请找出经过任意数量滑雪道(包括零条)后,以任意一个空间为终点时,他所能达到的最大幸福感。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

AtCoder Ski Area has NN open spaces called Space 11, Space 22, \ldots, Space NN. The altitude of Space ii is HiH_i. There are MM slopes that connect two spaces bidirectionally. The ii-th slope (1iM)(1 \leq i \leq M) connects Space UiU_i and Space ViV_i. It is possible to travel between any two spaces using some slopes.

Takahashi can only travel between spaces by using slopes. Each time he goes through a slope, his happiness changes. Specifically, when he goes from Space XX to Space YY by using the slope that directly connects them, his happiness changes as follows.

  • If the altitude of Space XX is strictly higher than that of Space YY, the happiness increases by their difference: HXHYH_X-H_Y.
  • If the altitude of Space XX is strictly lower than that of Space YY, the happiness decreases by their difference multiplied by 22: 2(HYHX)2(H_Y-H_X).
  • If the altitude of Space XX is equal to that of Space YY, the happiness does not change.

The happiness may be a negative value.

Initially, Takahashi is in Space 11, and his happiness is 00. Find his maximum possible happiness after going through any number of slopes (possibly zero), ending in any space.

Constraints

  • 2N2×1052 \leq N \leq 2\times 10^5
  • $N-1 \leq M \leq \min( 2\times 10^5,\frac{N(N-1)}{2})$
  • 0Hi1080 \leq H_i\leq 10^8 (1iN)(1 \leq i \leq N)
  • 1Ui<ViN1 \leq U_i < V_i \leq N (1iM)(1 \leq i \leq M)
  • (Ui,Vi)(Uj,Vj)(U_i,V_i) \neq (U_j, V_j) if iji \neq j.
  • All values in input are integers.
  • It is possible to travel between any two spaces using some slopes.

Input

Input is given from Standard Input in the following format:

NN MM

H1H_1 H2H_2 \ldots HNH_N

U1U_1 V1V_1

U2U_2 V2V_2

\vdots

UMU_M VMV_M

Output

Print the answer.

Sample Input 1

4 4
10 8 12 5
1 2
1 3
2 3
3 4

Sample Output 1

3

If Takahashi takes the route Space 11 \to Space 33 \to Space 44, his happiness changes as follows.

  • When going from Space 11 (altitude 1010) to Space 33 (altitude 1212), it decreases by 2×(1210)=42\times (12-10)=4 and becomes 04=40-4=-4.
  • When going from Space 33 (altitude 1212) to Space 44 (altitude 55), it increases by 125=712-5=7 and becomes 4+7=3-4+7=3.

If he ends the travel here, the final happiness will be 33, which is the maximum possible value.

Sample Input 2

2 1
0 10
1 2

Sample Output 2

0

His happiness is maximized by not moving at all.

update @ 2024/3/10 10:16:00