#include <bits/stdc++.h>
using namespace std;
const int MM = 2e5 + 5, MOD = 1e9 + 7;
int N, M, rq[MM], hv[MM], cnt[MM];
vector<pair<int, int>> deals;
bool check(int d) {
  for (int i = 1; i <= N; i++) hv[i] = rq[i];
  memset(cnt, 0, sizeof(cnt));
  int cur = 1, day = 1;
  for (pair<int, int> x : deals) {
    if (x.first > d) break;
    cnt[x.second]++;
  }
  for (pair<int, int> x : deals) {
    if (x.first > d) break;
    if (cnt[x.second] > 1) {
      cnt[x.second]--;
      continue;
    }
    cur += x.first - day;
    day = x.first;
    int by = min(hv[x.second], cur);
    cur -= by;
    hv[x.second] -= by;
  }
  cur += d - day;
  for (int i = 1; i <= N; i++) cur -= hv[i] * 2;
  return cur >= 0;
}
int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> N >> M;
  for (int i = 1; i <= N; i++) cin >> rq[i];
  for (int i = 1, d, t; i <= M; i++) {
    cin >> d >> t;
    deals.push_back({d, t});
  }
  sort(begin(deals), end(deals));
  int lo = 1, hi = 1e6, mid;
  while (lo < hi) {
    mid = (lo + hi) / 2;
    if (check(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  cout << lo << "\n";
}
