#include long long int Max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int x[300005], y[300005]; long long int heap[300005], l; int comp_h(long long int a, long long int b) { if (x[heap[a]] > x[heap[b]]) return 1; else return -1; } void swap_h(long long int a, long long int b) { long long int f = heap[a]; heap[a] = heap[b]; heap[b] = f; return; } void push(long long int ne) { heap[l] = ne; long long int p = l; l++; for (; p > 0; p = (p - 1) / 2) if (comp_h((p - 1) / 2, p) > 0) swap_h((p - 1) / 2, p); return; } long long int pop() { l--; swap_h(0, l); long long int p = 0; for (;;) { if (2 * p + 2 < l) { if (comp_h(2 * p + 1, 2 * p + 2) > 0) { if (comp_h(p, 2 * p + 2) > 0) swap_h(p, 2 * p + 2); p = 2 * p + 2; } else { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } } else if (2 * p + 1 < l) { if (comp_h(p, 2 * p + 1) > 0) swap_h(p, 2 * p + 1); p = 2 * p + 1; } else break; } return heap[l]; } long long int n, m; long long int h[100005]; long long int c[300005]; long long int dp[100005][2]; long long int f1() { long long int i; for (i = 0; i < n; i++) dp[i][0] = dp[i][1] = -1; dp[0][0] = 0; long long int min, mid, max; for (i = 0; i < n; i++) { min = -1; max = m; while (max - min > 1) { mid = (max + min) / 2; if (x[c[mid]] < i) min = mid; else max = mid; } for (; x[c[max]] == i; max++) { if (y[c[max]] < i) continue; if (h[y[c[max]]] < h[i]) { if (dp[i][0] >= 0) dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][0]); if (dp[i][1] >= 0) dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][1]); } else { if (dp[i][0] >= 0) dp[y[c[max]]][1] = Max(dp[y[c[max]]][1], dp[i][0] + h[y[c[max]]] - h[i]); } } } return Max(dp[n - 1][0], dp[n - 1][1]); } long long int f2() { long long int i; for (i = 0; i < n; i++) dp[i][0] = dp[i][1] = -1; dp[n - 1][0] = 0; long long int min, mid, max; for (i = n - 1; i >= 0; i--) { min = -1; max = m; while (max - min > 1) { mid = (max + min) / 2; if (x[c[mid]] < i) min = mid; else max = mid; } for (; x[c[max]] == i; max++) { if (y[c[max]] > i) continue; if (h[y[c[max]]] < h[i]) { if (dp[i][0] >= 0) dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][0]); if (dp[i][1] >= 0) dp[y[c[max]]][0] = Max(dp[y[c[max]]][0], dp[i][1]); } else { if (dp[i][0] >= 0) dp[y[c[max]]][1] = Max(dp[y[c[max]]][1], dp[i][0] + h[y[c[max]]] - h[i]); } } } return Max(dp[0][0], dp[0][1]); } int main() { scanf("%lld %lld", &n, &m); long long int i; for (i = 0; i < n; i++) scanf("%lld", &h[i]); for (i = 0; i < m; i++) { scanf("%lld %lld", &x[i], &y[i]); x[i]--; y[i]--; x[i + m] = y[i]; y[i + m] = x[i]; } l = 0; m *= 2; for (i = 0; i < m; i++) push(i); for (i = 0; i < m; i++) c[i] = pop(); c[m] = m; x[m] = -1; printf("%lld\n%lld\n", f1(), f2()); return 0; }