#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> kol(n);
  int max_ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> kol[i];
    max_ans += kol[i];
  }
  vector<vector<int> > sales(n);
  for (int i = 0; i < m; i++) {
    int type, day;
    cin >> day >> type;
    sales[type - 1].push_back(day - 1);
  }
  int l = 0, r = 2 * max_ans;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    vector<int> p_sales(mid + 1, 0);
    for (int type = 0; type < n; type++) {
      int mx = -1;
      for (int i : sales[type]) {
        if (mid > i) mx = max(mx, i);
      }
      if (mx != -1) p_sales[mx] += kol[type];
    }
    int money = 0, sv = 0;
    for (int i = 0; i < mid; i++) {
      money++;
      int x = min(money, p_sales[i]);
      money -= x;
      sv += x;
    }
    int need = 2 * max_ans - sv;
    if (need <= mid)
      r = mid;
    else
      l = mid;
  }
  cout << r;
}
