#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e18;
const long long minN = -1e18;
const int INF = 2e9;
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const long double PI = acos(-1);
const int limit = 4e5 + 5;
const int limit1 = 1e6 + 5;
const int limit2 = 1e3 + 5;
pair<int, int> dir[] = {{0, 1}, {1, 0},  {0, -1}, {-1, 0},
                        {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
pair<int, int> NON = {-1, -1};
int n, m;
int need[limit];
vector<int> g[limit];
int day[limit], type[limit];
bool canOrder(int maxDay) {
  vector<int> remain(need, need + n + 1);
  vector<int> lastDay(n + 5);
  int burles = 0;
  for (int i = (1); i <= (m); ++i) {
    if (day[i] <= maxDay) {
      lastDay[type[i]] = max(lastDay[type[i]], day[i]);
    }
  }
  for (int i = (1); i <= (n); ++i) {
    if (lastDay[i]) g[lastDay[i]].push_back(i);
  }
  for (int i = (1); i <= (maxDay); ++i) {
    ++burles;
    if (!g[i].empty()) {
      for (auto it : g[i]) {
        if (remain[it]) {
          int t = min(remain[it], burles);
          burles -= t;
          remain[it] -= t;
        }
      }
    }
  }
  for (int i = (1); i <= (n); ++i) {
    burles -= 2 * remain[i];
  }
  for (int i = (1); i <= (200000); ++i) g[i].clear();
  return burles >= 0;
}
void solveProblem() {
  int l = 1, r = 400000, ans = 400000;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (canOrder(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << '\n';
}
void fastInput() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void readInput() {
  cin >> n >> m;
  for (int i = (1); i <= (n); ++i) {
    cin >> need[i];
  }
  for (int i = (1); i <= (m); ++i) {
    cin >> day[i] >> type[i];
  }
}
int main() {
  fastInput();
  readInput();
  solveProblem();
}
