/* -*- coding: utf-8 -*- * * 1008.cc: No.1008 Bench Craftsman - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_M = 100000; const int MAX_E2 = 1 << 18; // = 262144 const int INF = 1 << 30; /* typedef */ typedef long long ll; struct Elm { // a+bx ll a, b; Elm() {} Elm(ll _a, ll _b): a(_a), b(_b) {} bool operator==(const Elm &e) const { return a == e.a && b == e.b; } bool operator!=(const Elm &e) const { return a != e.a || b != e.b; } Elm operator+(const Elm &e) const { return Elm(a + e.a, b + e.b); } Elm &operator+=(const Elm &e) { a += e.a, b += e.b; return *this; } Elm operator*(const int x) const { return Elm(a + b * x, b); } }; template struct SegTreeSumDelay { int e2; T nodes[MAX_E2], z; int ls[MAX_E2]; SegTreeSumDelay() {} void init(int n, T _z) { z = _z; for (e2 = 1; e2 < n; e2 <<= 1); ls[0] = e2; for (int j = 0; j < e2 - 1; j++) ls[j * 2 + 1] = ls[j * 2 + 2] = (ls[j] >> 1); clear(); } void clear() { fill(nodes, nodes + e2 * 2, z); } T &get(int i) { return nodes[e2 - 1 + i]; } void __update(int k) { if (nodes[k] != z) { int k0 = k * 2 + 1, k1 = k0 + 1; nodes[k0] += nodes[k]; nodes[k1] += nodes[k] * ls[k0]; nodes[k] = z; } } void updateall() { for (int j = 0; j < e2 - 1; j++) __update(j); } void add_range(int r0, int r1, T v, int k, int i0, int i1) { if (r1 <= i0 || i1 <= r0) return; if (r0 <= i0 && i1 <= r1) { nodes[k] += v * (i0 - r0); return; } __update(k); int im = (i0 + i1) / 2; int k0 = k * 2 + 1, k1 = k0 + 1; add_range(r0, r1, v, k0, i0, im); add_range(r0, r1, v, k1, im, i1); } void add_range(int r0, int r1, T v) { //printf(" add_range(%d,%d,(%lld,%lld))\n", r0, r1, v.a, v.b); add_range(r0, r1, v, 0, 0, e2); } }; /* global variables */ int as[MAX_N], xis[MAX_M], wis[MAX_M]; SegTreeSumDelay st; /* subroutines */ bool check(int n, int m, int c) { st.clear(); for (int i = 0; i < m; i++) { int xi = xis[i], wi = wis[i]; int l = (wi - 1) / c; if (l == 0) st.add_range(xi, xi + 1, Elm(wi, 0)); else { st.add_range(xi - l, xi + 1, Elm(wi - c * l, c)); st.add_range(xi + 1, xi + l + 1, Elm(wi - c, -c)); } } st.updateall(); //printf("c=%d:", c); //for (int i = 0; i < n; i++) printf(" %lld", st.get(i).a); putchar('\n'); for (int i = 0; i < n; i++) if (as[i] <= st.get(i).a) return false; return true; } /* main */ int main() { int n, m; scanf("%d%d", &n, &m); int mina = INF; for (int i = 0; i < n; i++) { scanf("%d", as + i); if (mina > as[i]) mina = as[i]; } int minw = INF, maxw = 0; ll wsum = 0; for (int i = 0; i < m; i++) { scanf("%d%d", xis + i, wis + i); xis[i]--; if (as[xis[i]] <= wis[i]) { puts("-1"); return 0; } if (maxw < wis[i]) maxw = wis[i]; wsum += wis[i]; } if (mina > wsum) { puts("0"); return 0; } //printf("mina=%d, maxw=%d, wsum=%lld\n", mina, maxw, wsum); st.init(n, Elm(0, 0)); int c0 = 0, c1 = maxw; while (c0 + 1 < c1) { int c = (c0 + c1) / 2; if (check(n, m, c)) c1 = c; else c0 = c; } printf("%d\n", c1); return 0; }