#include <bits/stdc++.h>
using namespace std;
const int N = 4e5;
vector<int> sale[N];
int n, m, d, t, ans, l, r, mid, npos[N];
struct node {
  int mark, tot;
} a[N];
bool cmp(node a, node b) {
  return sale[a.mark][npos[a.mark]] < sale[b.mark][npos[b.mark]];
}
bool check(int z) {
  int money = z;
  memset(npos, 0, sizeof(npos));
  for (int i = 1; i <= n; i++) {
    npos[i] = sale[i].size() - 1;
    int ll = 1, rr = sale[i].size() - 1, midd, temp = N;
    while (ll <= rr) {
      midd = (ll + rr) >> 1;
      if (sale[i][midd] > z)
        temp = midd, rr = midd - 1;
      else
        ll = midd + 1;
    }
    if (temp != N) npos[i] = temp - 1;
  }
  sort(a + 1, a + n + 1, cmp);
  int cost = 0, re = 0;
  for (int i = 1; i <= n; i++) {
    int hm = sale[a[i].mark][npos[a[i].mark]] - cost;
    if (hm >= a[i].tot) {
      money -= a[i].tot;
      cost += a[i].tot;
    } else {
      money -= hm;
      re += a[i].tot - hm;
      cost += hm;
    }
  }
  money -= re * 2;
  return money >= 0 ? 1 : 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) a[i].mark = i, sale[i].push_back(0);
  for (int i = 1; i <= n; i++) cin >> a[i].tot;
  for (int i = 1; i <= m; i++) {
    cin >> d >> t;
    sale[t].push_back(d);
  }
  for (int i = 1; i <= n; i++) sort(sale[i].begin(), sale[i].end());
  l = 0, r = 400001;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1, ans = mid;
    else
      l = mid + 1;
  }
  printf("%d", ans);
  return 0;
}
