#include <bits/stdc++.h>
using namespace std;
const long long int N = 5E5;
long long int mx[N];
set<long long int> d[N];
long long int n, k, total, coins;
bool pos(long long int day) {
  long long int remov = day - coins;
  long long int f = 0;
  if (remov < 0) return false;
  remov = coins - remov;
  remov = max((long long int)0, remov);
  long long int cnt = 0;
  if (cnt >= remov) return true;
  long long int arr[n];
  for (long long int i = 0; i < n; i++) {
    arr[i] = 0;
  }
  for (long long int i = day; i >= 1; i--) {
    for (auto k : d[i]) {
      long long int type = k;
      long long int take = mx[type] - arr[type];
      long long int ds = i - f;
      if (ds <= 0) return false;
      if (take > ds) {
        take = ds;
      }
      f += take;
      arr[type] += take;
      cnt += (take);
      if (cnt >= remov) return true;
    }
    f--;
    f = max(f, (long long int)0);
  }
  if (cnt >= remov) return true;
  return false;
}
int main() {
  cin >> n >> k;
  total = 0;
  coins = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> mx[i];
    coins += mx[i];
    total += (mx[i] * 2);
  }
  for (long long int i = 0; i < k; i++) {
    long long int day, type;
    cin >> day >> type;
    type--;
    if (mx[type] != 0) d[day].insert(type);
  }
  bool ch = pos((long long int)20);
  long long int low = 1, high = total;
  while (low < high) {
    long long int mid = (low + high) / 2;
    if (pos(mid))
      high = mid;
    else
      low = mid + 1;
  }
  cout << low;
  return 0;
}
