#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f;
const long long inf = INF;
long long modulo = 998244353;
pair<long long, long long> offer[2000100];
long long need[2000100];
long long when[2000100];
long long day[2000100];
void deal() {
  int n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> need[i];
  for (long long i = 0; i < m; i++) {
    int d, t;
    cin >> d >> t;
    offer[i].first = d, offer[i].second = t - 1;
  }
  int a = 0, b = pow(10, 6);
  while (a < b) {
    int c = (a + b) / 2;
    for (long long i = 0; i < n; i++) when[i] = -1;
    for (long long i = 0; i < m; i++)
      if (offer[i].first <= c)
        when[offer[i].second] = max(when[offer[i].second], offer[i].first);
    for (long long i = 0; i < 2000100; i++) day[i] = 0;
    for (long long i = 0; i < n; i++)
      if (when[i] != -1) day[when[i]] += need[i];
    long long total = 0;
    for (long long i = 0; i < n; i++) total += need[i];
    long long sum = 0;
    long long money = 0;
    for (int i = 1; i < c + 1; i++) {
      ++money;
      sum += min(money, day[i]);
      money -= min(money, day[i]);
    }
    long long left = total - sum;
    if (money >= left * 2)
      b = c;
    else
      a = c + 1;
  }
  cout << a;
}
int main() { deal(); }
