#include #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); } template void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); } namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } #define mv_rec make_v(init, tail...) template T make_v(T init) { return init; } template auto make_v(T init, size_t s, Tail... tail) { return vec(s, mv_rec); } #undef mv_rec namespace ios { char line = '\n'; } template std::ostream& operator <<(std::ostream &os, const vec &v) { for (ll i = 0; i < v.size(); i++) os << v[i] << " \n"[i + 1 == v.size()]; return os; } struct Solver { ll n, m; vec av; vec xw; vec imos1, imos2; constexpr static std::size_t SIZE = 2e5 + 10; Solver(ll n, ll m) : n(n), m(m), av(n), xw(m), imos1(2 * SIZE), imos2(2 * SIZE) { for (auto &&e : av) std::cin >> e; for (auto &&e : xw) { ll x, w; std::cin >> x >> w; e = pll(x - 1, w); } } bool check(ll c) { fill(ALL(imos1), 0); fill(ALL(imos2), 0); auto calc_len = [&](ll w) { return (c == 0 ? n + 5 : w / c); }; for (ll j = 0; j < m; j++) { ll x, w; std::tie(x, w) = xw[j]; ll len = calc_len(w); imos1[x + 1] -= c; imos1[x + 1 + len] += c; imos2[x - 1 + SIZE] -= c; imos2[x - 1 - len + SIZE] += c; } auto fold_imos = [&] { for (ll i = 0; i + 1 < imos1.size(); i++) imos1[i + 1] += imos1[i]; for (ll i = imos2.size() - 1; 0 <= i - 1; i--) imos2[i - 1] += imos2[i]; }; fold_imos(); for (ll j = 0; j < m; j++) { ll x, w; std::tie(x, w) = xw[j]; ll len = calc_len(w); ll sumv = c * len; imos1[x] += w; imos1[x + 1 + len] += -w + sumv; imos2[x + SIZE] += w; imos2[x - 1 - len + SIZE] += -w + sumv; } fold_imos(); for (ll j = 0; j < m; j++) { ll x, w; std::tie(x, w) = xw[j]; imos2[x + SIZE] -= w; } for (ll i = 0; i < n; i++) { ll a = av[i]; a -= imos1[i]; a -= imos2[i + SIZE]; if (a <= 0) return false; } return true; } ll solve() { ll init = 1e5 + 10; ll ok = init, ng = -1; while (ok - ng > 1) { ll mid = (ok + ng) / 2; (check(mid) ? ok : ng) = mid; } return (ok == init ? -1 : ok); } }; int main() { ll n, m; std::cin >> n >> m; Solver solver(n, m); std::cout << solver.solve() << ios::line; return 0; }