#define _CRT_SECURE_NO_WARNINGS #pragma comment (linker, "/STACK:526000000") #include "bits/stdc++.h" using namespace std; typedef string::const_iterator State; #define eps 1e-11L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define seg_size 262144 * 4LL #define pb push_back #define mp make_pair typedef long long ll; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() void init() { iostream::sync_with_stdio(false); cout << fixed << setprecision(20); } #define int ll vector inputs; vector mans[200000]; int n, m; int check(int border) { vector color; REP(i, n) { color.push_back(0); } { priority_queue> next; int cnt = 0; int now = 0; for (int q = n - 1; q >= 0; --q) { for (auto x : mans[q]) { next.push(mp(q - (x+border) / border, x % border)); now += x + border; cnt++; } while (next.empty() == false&&next.top().first == q) { cnt--; now -= next.top().second; next.pop(); } now -= cnt * border; color[q] += now; } } { priority_queue> next; int cnt = 0; int now = 0; for (int q = 1; q < n; ++q) { for (auto x : mans[q-1]) { next.push(mp(-((q-1) + (x + border ) / border), x % border)); now += x; cnt++; } while (next.empty() == false&&next.top().first == -q) { cnt--; now -= next.top().second; next.pop(); } now -= cnt * border; color[q] += now; } } REP(i, n) { if (inputs[i] <= color[i]) return 0; } return 1; } void solve() { cin >> n >> m; REP(i, n) { int a; cin >> a; inputs.push_back(a); } int hoge = 0; REP(i, m) { int a, b; cin >> a >> b; a--; mans[a].push_back(b); hoge += b; } //check(2); int ok = 1; REP(i, inputs.size()) { if (hoge >= inputs[i]) { ok = 0; } } if (ok == 1) { cout << 0 << endl; return; } int bot = 1e9; int top = 0; while (bot - top > 1) { int mid = (top + bot) / 2; if (check(mid)) { bot = mid; } else { top = mid; } } if (bot == 1e9) bot = -1; cout << bot << endl; } #undef int int main() { init(); solve(); }