#include <bits/stdc++.h>
using namespace std;
void sorta(long long sr[][2], long long n) {
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      if (sr[j][0] < sr[i][0]) {
        swap(sr[j][0], sr[i][0]);
        swap(sr[j][1], sr[i][1]);
      } else if (sr[j][0] == sr[i][0]) {
        if (sr[j][1] < sr[i][1]) {
          swap(sr[i][1], sr[j][1]);
        }
      }
    }
  }
}
bool check(long long days, long long maxd, long long ar[], long long n,
           long long sr[][2], long long m) {
  long long scr[n][2];
  for (long long i = 0; i < n; i++) {
    scr[i][0] = i + 1;
    scr[i][1] = -1;
  }
  for (long long i = 0; i < m; i++) {
    if (sr[i][0] > days) continue;
    scr[sr[i][1] - 1][1] = max(scr[sr[i][1] - 1][1], sr[i][0]);
  }
  map<long long, vector<long long>> offer;
  for (long long i = 0; i < n; i++) {
    if (scr[i][1] != -1) {
      offer[scr[i][1]].push_back(scr[i][0]);
    }
  }
  long long used = 0;
  for (long long i = 1; i <= days; i++) {
    long long l = offer[i].size();
    for (long long j = 0; j < l; j++) {
      if (ar[offer[i][j] - 1] > 0) {
        if (i - used >= ar[offer[i][j] - 1]) {
          used += ar[offer[i][j] - 1];
        } else {
          used = i;
        }
      }
    }
  }
  return days + used >= maxd;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long ar[n];
  long long maxd = 0;
  for (long long i = 0; i < n; i++) {
    cin >> ar[i];
    maxd += ar[i] * 2;
  }
  long long sr[m][2];
  for (long long i = 0; i < m; i++) {
    cin >> sr[i][0] >> sr[i][1];
  }
  long long days = 1;
  long long l = 1, r = maxd, mid;
  while (r - l > 2) {
    mid = (l + r) / 2;
    if (check(mid, maxd, ar, n, sr, m))
      r = mid;
    else
      l = mid;
  }
  for (days = l; days <= r; days++) {
    if (check(days, maxd, ar, n, sr, m)) {
      maxd = days;
      break;
    }
  }
  cout << maxd;
  return 0;
}
