#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int c[maxn];
struct sale {
  int day;
  int kind;
} b[maxn];
set<int> s;
int tot = 0;
int n, m;
bool cmp(sale x, sale y) {
  return x.day == y.day ? (x.kind < y.kind) : (x.day < y.day);
}
int k[maxn], tmp[maxn];
bool judge(int ans) {
  memcpy(tmp + 1, k + 1, sizeof(int) * n);
  int money = 0, cost = 0;
  for (int i = 0; i < tot; i++) {
    if (b[i].day > ans) break;
    c[b[i].kind] = b[i].day;
  }
  for (int i = 1; i <= ans; i++) {
    money++;
    for (int j = 1; j <= n && money; j++)
      while (money && tmp[j] && c[j] == i) {
        money--;
        cost++;
        tmp[j]--;
      }
  }
  for (int i = 1; i <= n; i++) cost += tmp[i] * 2;
  if (cost <= ans)
    return true;
  else
    return false;
}
int main() {
  scanf("%d%d", &n, &m);
  int sum = 0;
  s.clear();
  for (int i = 1; i <= n; i++) {
    scanf("%d", &k[i]);
    if (k[i] == 0) s.insert(i);
    sum += k[i];
  }
  for (int i = 0; i < m; i++) {
    int p, q;
    scanf("%d%d", &p, &q);
    if (s.count(q)) continue;
    b[tot].day = p;
    b[tot].kind = q;
    tot++;
  }
  sort(b, b + tot, cmp);
  int low = 1, high = sum * 2;
  while (low < high) {
    int mid = low + (high - low) / 2;
    if (judge(mid))
      high = mid;
    else
      low = mid + 1;
  }
  cout << low << endl;
  return 0;
}
