#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, sz[N], m, sum, backup[N], last[N];
pair<int, int> p[N], t[N];
bool check(int x) {
  int c = 0, now = 0, ss = sum, cnt = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  memset(last, 0, sizeof last);
  for (int i = 1; i <= m; i++)
    if (p[i].first <= x) last[p[i].second] = p[i].first;
  for (int i = 1; i <= n; i++)
    if (last[i]) t[++cnt] = {last[i], i};
  for (int i = 1; i <= cnt; i++) q.push(t[i]);
  memcpy(sz, backup, sizeof backup);
  while (now < x) {
    now++, c++;
    while (q.size() && q.top().first < now) q.pop();
    while (c && q.size() && q.top().first == now) {
      pair<int, int> it = q.top();
      q.pop();
      if (!sz[it.second]) continue;
      int y = min(sz[it.second], c);
      c -= y;
      sz[it.second] -= y;
      ss -= y;
      if (!c) break;
    }
  }
  if (ss && c >= 2 * ss) return 1;
  if (!ss) return 1;
  return 0;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &sz[i]), sum += sz[i];
  for (int i = 1; i <= m; i++) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p + 1, p + m + 1);
  memcpy(backup, sz, sizeof sz);
  int l = sum, r = sum << 1;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", r);
  return 0;
}
