#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[200200];
int main() {
  scanf("%d %d", &n, &m);
  int total_buy = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
    total_buy += arr[i];
  }
  pair<int, int> promo[200200];
  for (int i = 0; i < m; i++) {
    int d, t;
    scanf("%d %d", &d, &t);
    promo[i].first = d;
    promo[i].second = t;
  }
  int l = 0, r = 400001;
  while (l < r) {
    int mid = (l + r) / 2;
    int last_promo[200200];
    memset(last_promo, 0, sizeof last_promo);
    for (int i = 0; i < m; i++) {
      int d = promo[i].first, t = promo[i].second;
      if (promo[i].first <= mid) last_promo[t] = max(last_promo[t], d);
    }
    int must_buy[200200];
    memset(must_buy, 0, sizeof must_buy);
    for (int i = 1; i <= n; i++) {
      must_buy[last_promo[i]] += arr[i];
    }
    int money = 0;
    int bought = 0;
    for (int i = 1; i <= mid; i++) {
      money++;
      if (i > 200000) continue;
      if (money > must_buy[i]) {
        bought += must_buy[i];
        money -= must_buy[i];
      } else {
        bought += money;
        money = 0;
      }
    }
    if (money >= 2 * (total_buy - bought)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d\n", l);
}
