import sys input = sys.stdin.buffer.readline def accumulate(X): Y = [0] * len(X) Y[0] = X[0] for i in range(1, len(X)): Y[i] = Y[i - 1] + X[i] return Y def isOK(c): imos = [0] * (N + 2) for x, w in people: left = max(x - (w // c), 0) imos[left] += w - c * (x - left) imos[left + 1] += c - (w - c * (x - left)) imos[x + 1] -= 2 * c right = min(x + (w // c), N - 1) imos[right + 1] += c - (w - c * (right - x)) imos[right + 2] += w - c * (right - x) B = accumulate(accumulate(imos)) for i in range(N): if B[i] >= A[i]: return False return True def solve(N, A, people): # 耐久度0 if min(A) > sum(w for _, w in people): return 0 # 不可能 direct = [0] * N for x, w in people: direct[x] += w for i in range(N): if direct[i] >= A[i]: return -1 l, r = 0, 10 ** 9 while r - l > 1: m = (r + l) // 2 if isOK(m): r = m else: l = m return r N, M = map(int, input().split()) A = tuple(map(int, input().split())) people = [] for _ in range(M): x, w = map(int, input().split()) x -= 1 people.append((x, w)) print(solve(N, A, people))