#include #include #include using namespace std; typedef long long ll; template struct edge { int from, to; T cost; int id; operator int() const { return to; } }; template struct graph { int n, m; std::vector>> g; graph() {} graph(int n) : n(n), m(0) { g.resize(n); } void add_directed_edge(int from, int to, T cost = 1) { assert(0 <= from && from < n); assert(0 <= to && to < n); g[from].push_back((edge){from, to, cost, m++}); } void add_undirected_edge(int from, int to, T cost = 1) { assert(0 <= from && from < n); assert(0 <= to && to < n); g[from].push_back((edge){from, to, cost, m}); g[to].push_back((edge){to, from, cost, m++}); } int size() { return n; } int edge_size() { return m; } inline const std::vector> &operator[](const int &u) const { return g[u]; } inline std::vector> &operator[](const int &u) { return g[u]; } }; int main() { int n, m; cin >> n >> m; ll h[100005]; for (int u = 0; u < n; u++) { cin >> h[u]; } graph g(n * 2), r(n * 2); for (int u = 0; u < n; u++) { g.add_directed_edge(u * 2, u * 2 + 1, 0); r.add_directed_edge(u * 2 + 1, u * 2, 0); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; if (x > y) { swap(x, y); } if (h[x] < h[y]) { g.add_directed_edge(x * 2, y * 2 + 1, h[y] - h[x]); r.add_directed_edge(y * 2, x * 2 + 1, 0); } else { g.add_directed_edge(x * 2 + 1, y * 2, 0); r.add_directed_edge(y * 2 + 1, x * 2, h[x] - h[y]); } } ll d0[200005], d1[200005]; for (int u = 0; u < n * 2; u++) { d0[u] = d1[u] = -1; } d0[0] = 0; for (int u = 0; u < n * 2; u++) { if (d0[u] == -1) { continue; } for (edge e : g[u]) { d0[e.to] = max(d0[e.to], d0[u] + e.cost); } } cout << d0[n * 2 - 1] << endl; d1[n * 2 - 1] = 0; for (int u = n * 2 - 1; u >= 0; u--) { if (d1[u] == -1) { continue; } for (edge e : r[u]) { d1[e.to] = max(d1[e.to], d1[u] + e.cost); } } cout << d1[0] << endl; }