import sys input = sys.stdin.buffer.readline def isOK(c): queries = [[] for _ in range(N + 1)] for x, w in people: left = max(x - (w // c), 0) queries[left].append((1, w - c * (x - left))) queries[x].append((2,)) right = min(x + (w // c), N) queries[right].append((3, w - c * (right - x))) positive = 0 cur = 0 for i in range(N): cur += positive * c for q in queries[i]: if q[0] == 1: positive += 1 cur += q[1] elif q[0] == 2: positive -= 2 else: # assert q[0] == 3 positive += 1 cur -= q[1] if cur >= A[i]: return False return True def solve(N, M, 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, M, A, people))