atcoder#ABC188E. [ABC188E] Peddler

[ABC188E] Peddler

Score : 500500 points

Problem Statement

In Takahashi Kingdom, there are NN towns, called Town 11 through Town NN. There are also MM roads in the kingdom, called Road 11 through Road MM. By traversing Road ii, you can travel from Town XiX_i to Town YiY_i, but not vice versa. Here, it is guaranteed that Xi<YiX_i < Y_i. Gold is actively traded in this kingdom. At Town ii, you can buy or sell 11 kilogram of gold for AiA_i yen (the currency of Japan).

Takahashi, a traveling salesman, plans to buy 11 kilogram of gold at some town, traverse one or more roads, and sell 11 kilogram of gold at another town. Find the maximum possible profit (that is, the selling price minus the buying price) in this plan.

Constraints

  • 2N2×1052 \le N \le 2 \times 10^5
  • 1M2×1051 \le M \le 2 \times 10^5
  • 1Ai1091 \le A_i \le 10^9
  • 1Xi<YiN1 \le X_i \lt Y_i \le N
  • (Xi,Yi)(Xj,Yj)(ij)(X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM

A1A_1 A2A_2 A3A_3 \dots ANA_N

X1X_1 Y1Y_1

X2X_2 Y2Y_2

X3X_3 Y3Y_3

\hspace{15pt} \vdots

XMX_M YMY_M

Output

Print the answer.

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

We can achieve the profit of 33 yen, as follows:

  • At Town 11, buy one kilogram of gold for 22 yen.
  • Traverse Road 22 to get to Town 22.
  • Traverse Road 11 to get to Town 44.
  • At Town 44, sell one kilogram of gold for 55 yen.
5 5
13 8 3 15 18
2 4
1 2
4 5
2 3
1 3
10

We can achieve the profit of 1010 yen, as follows:

  • At Town 22, buy one kilogram of gold for 88 yen.
  • Traverse Road 11 to get to Town 44.
  • Traverse Road 33 to get to Town 55.
  • At Town 55, sell one kilogram of gold for 1818 yen.
3 1
1 100 1
2 3
-99

Note that we cannot sell gold at the town where we bought the gold, which means the answer may be negative.