#include <bits/stdc++.h>
using namespace std;
int n, m, v[200005];
vector<int> ed[200005];
bool can(int ans) {
  if (ans > 400005) return true;
  vector<int> temp[ans + 1];
  for (int i = 0; i < n; i++) {
    int cnt = !!v[i + 1];
    for (int days : ed[i + 1]) {
      if (!cnt) break;
      if (days > ans) continue;
      temp[days].push_back(i + 1), cnt--;
    }
  }
  int bought = 0, cost = 0;
  for (int i = 0; i < ans; i++)
    for (int ty : temp[i + 1]) bought += min(i + 1 - bought, v[ty]);
  for (int i = 0; i < n; i++) cost += v[i + 1] * 2;
  return cost - bought <= ans;
}
int main(int argc, char **argv) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> v[i + 1];
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    ed[b].push_back(a);
  }
  for (int i = 0; i < n; i++) sort(ed[i + 1].rbegin(), ed[i + 1].rend());
  int ans = 1 << 19;
  for (int i = 18; i >= 0; i--)
    if (can(ans - (1 << i))) ans -= 1 << i;
  cout << ans << endl;
}
