#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
using ll = long long;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> k(n);
  int sum_k = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &k[i]);
    sum_k += k[i];
  }
  vector<vector<int>> sales(n);
  while (m--) {
    int day, type;
    scanf("%d%d", &day, &type);
    assert(1 <= type && type <= n);
    sales[type - 1].push_back(day - 1);
  }
  int low = 0, high = 2 * sum_k;
  while (low < high) {
    int duration = (low + high) / 2;
    vector<int> possible_sales(duration);
    for (int type = 0; type < n; ++type) {
      int big = -1;
      for (int day : sales[type]) {
        if (day < duration) {
          big = max(big, day);
        }
      }
      if (big != -1) {
        possible_sales[big] += k[type];
      }
    }
    int money = 0, saved = 0;
    for (int day = 0; day < duration; ++day) {
      money++;
      int use_here_on_sale = min(money, possible_sales[day]);
      money -= use_here_on_sale;
      saved += use_here_on_sale;
    }
    int total_money_needed = 2 * sum_k - saved;
    debug() << " ["
            << "duration"
               ": "
            << (duration)
            << "] "
               " ["
            << "saved"
               ": "
            << (saved)
            << "] "
               " ["
            << "total_money_needed"
               ": "
            << (total_money_needed) << "] ";
    if (total_money_needed <= duration) {
      high = duration;
    } else {
      low = duration + 1;
    }
  }
  printf("%d\n", low);
}
