#include <bits/stdc++.h>
using namespace std;
int n, m, sum;
int k[200010], need[200010];
pair<int, int> q[200001];
bool last[200100], flag[200100];
bool operator<(pair<int, int>& a, pair<int, int>& b) {
  if (a.first == b.first) return a.second < b.second;
  return a.first < b.first;
}
bool can(int day) {
  memset(last, false, sizeof last);
  memset(flag, false, sizeof flag);
  for (int i = m - 1; i >= 0; i--)
    if (q[i].first <= day && !flag[q[i].second]) {
      flag[q[i].second] = true;
      last[i] = true;
    }
  memcpy(need, k, (sizeof(int)) * (n + 1));
  int cur_money = 0;
  int last_t = 0;
  int ans = sum * 2;
  for (int i = 0; i < m; ++i)
    if (last[i]) {
      cur_money += q[i].first - last_t;
      if (cur_money > need[q[i].second]) {
        cur_money -= need[q[i].second];
        ans -= need[q[i].second];
        need[q[i].second] = 0;
      } else {
        need[q[i].second] -= cur_money;
        ans -= cur_money;
        cur_money = 0;
      }
      last_t = q[i].first;
    }
  return ans <= day;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &k[i]);
    sum += k[i];
  }
  for (int i = 0; i < m; ++i) {
    scanf("%d%d", &q[i].first, &q[i].second);
    --q[i].second;
  }
  sort(q, q + m);
  can(7);
  int l = 0, r = 2000000001;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (can(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
  return 0;
}
