local mmi, mma = math.min, math.max local n, m = io.read("*n", "*n") local h = {} local edge = {} local invedge = {} for i = 1, n do h[i] = io.read("*n") edge[i] = {} invedge[i] = {} end for i = 1, m do local x, y = io.read("*n", "*n") table.insert(edge[x], y) table.insert(invedge[y], x) end local function solve() local upped = {0} local downed = {0} for i = 2, n do upped[i], downed[i] = -1, -1 end for i = 1, n - 1 do for j = 1, #edge[i] do local dst = edge[i][j] if h[i] < h[dst] and 0 <= downed[i] then upped[dst] = mma(upped[dst], downed[i] + h[dst] - h[i]) end if h[dst] < h[i] and 0 <= upped[i] then downed[dst] = mma(downed[dst], upped[i]) end if h[dst] < h[i] and 0 <= downed[i] then downed[dst] = mma(downed[dst], downed[i]) end end end print(mma(downed[n], upped[n])) end solve() for i = 1, n do edge[i] = {} local z = n + 1 - i if i < z then h[i], h[z] = h[z], h[i] end end for i = 1, n do for j = 1, #invedge[i] do local dst = invedge[i][j] table.insert(edge[n + 1 - i], n + 1 - dst) end end solve()