#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
  long long res = 1;
  a = a % 1000000007;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % 1000000007;
      b--;
    }
    a = (a * a) % 1000000007;
    b >>= 1;
  }
  return res;
}
long long fermat_inv(long long y) { return power(y, 1000000007 - 2ll); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    int n, m;
    cin >> n >> m;
    int a[n + 1];
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    vector<pair<int, int>> v(m, {0, 0});
    for (int i = 0; i < m; i++) cin >> v[i].first >> v[i].second;
    sort(v.begin(), v.end());
    int ans = 4e5 + 1;
    int l = 1;
    int h = 4e5;
    while (l <= h) {
      int mid = (l + h) / 2;
      int ctr = sum;
      int val = mid;
      int b[n + 1];
      for (int i = 1; i <= n; i++) b[i] = a[i];
      for (int i = m - 1; i >= 0; i--) {
        if (v[i].first > mid) continue;
        val = min(val, v[i].first);
        int val1 = min(b[v[i].second], val);
        val -= val1;
        b[v[i].second] -= val1;
        ctr -= val1;
      }
      if ((sum - ctr) + 2 * ctr <= mid) {
        ans = mid;
        h = mid - 1;
      } else
        l = mid + 1;
    }
    cout << ans << "\n";
  }
  return 0;
}
