#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np # %% N, M = map(int, readline().split()) A = np.array(readline().split(), np.int64) XW = np.array(read().split(), np.int64) X = XW[::2] - 1 W = XW[1::2] # %% def calc_cost(N, c, X, W): B1_d = np.zeros(N + 1, np.int64) B0_d = np.zeros(N + 1, np.int64) L = X - (W + c - 1) // c + 1 L = np.maximum(0, L) const_term = W - c * X np.add.at(B1_d, L, c) np.add.at(B0_d, L, const_term) np.add.at(B1_d, X+1, -c) np.add.at(B0_d, X + 1, -const_term) R = X + (W + c - 1) // c - 1 R = np.minimum(R, N - 1) const_term = W + c * X np.add.at(B1_d, X + 1, -c) np.add.at(B0_d, X + 1, const_term) np.add.at(B1_d, R + 1, c) np.add.at(B0_d, R + 1, -const_term) B0 = np.cumsum(B0_d)[:-1] B1 = np.cumsum(B1_d)[:-1] return B0 + B1 * np.arange(N, dtype=np.int64) def test(c): if c == 0: return W.sum() <= A.min() cost = calc_cost(N, c, X, W) return np.all(cost < A) # %% left = -1 right = 10 ** 5 + 10 while left + 1 < right: mid = (left + right) // 2 if test(mid): right = mid else: left = mid if right > 10 ** 5: print(-1) else: print(right)