#include using namespace std; typedef long long ll; typedef pair P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) templateostream& operator<<(ostream& os,const pair& a) {os << "(" << a.first << ", " << a.second << ")";return os;} int main() { int n,m; cin >> n >> m; vector a(n),w(m),sum(n,0); vector x(m); for (int i = 0;i < n;++i) scanf("%lld",&a[i]); for (int i = 0;i < m;++i) scanf("%d%lld",&x[i],&w[i]),x[i]--; for (int i = 0;i < m;++i) sum[x[i]] += w[i]; for (int i = 0;i < n;++i) if (a[i] <= sum[i]) { cout << -1 << endl; return 0; } ll s = 0; for (int i = 0;i < m;++i) s += w[i]; bool f = true; for (int i = 0;i < n;++i) if (a[i] <= s) {f = false;break;} if (f) { cout << 0 << endl; return 0; } ll mn = 0,mx = 1e9; while (mx-mn > 1) { int mid = (mn+mx)/2; vector sum1(n,0),sum2(n,0),sum3(n,0); for (int i = 0;i < m;++i) { int c = w[i]/mid; sum1[max(0,x[i]-c)] += w[i]; if (x[i]+c+1 < n) sum1[x[i]+c+1] -= w[i]; if (x[i]+1 < n) sum2[x[i]+1] += mid; if (x[i]+1+c < n) sum2[x[i]+c+1] -= mid; if (x[i]-1 >= 0) sum3[x[i]-1] += mid; if (x[i]-1-c >= 0) sum3[x[i]-1-c] -= mid; } for (int i = 1;i < n;++i) sum1[i] += sum1[i-1]; for (int i = 1;i < n;++i) sum2[i] += sum2[i-1]; for (int i = n-1;i > 0;--i) sum3[i-1] += sum3[i]; for (int i = 0;i < m;++i) { int c = w[i]/mid; if (x[i]+1+c < n) sum2[x[i]+c+1] -= (ll)c*mid; if (x[i]-c-1 >= 0) sum3[x[i]-c-1] -= (ll)c*mid; } for (int i = 1;i < n;++i) sum2[i] += sum2[i-1]; for (int i = n-1;i > 0;--i) sum3[i-1] += sum3[i]; bool ok = true; for (int i = 0;i < n;++i) if (sum1[i]-sum2[i]-sum3[i] >= a[i]) { ok = false; break; } (ok ? mx : mn) = mid; } cout << mx << endl; }