n, m = map(int, input().split()) a = list(map(int, input().split())) w = [0] * n for i in range(m): u, v = map(int, input().split()) w[u - 1] += v def ceildiv(a, b): return -(-a // b) def check(c): if c <= 0: return sum(w) <= min(a) pt = [] for j in range(n): u = (w[j] + c * j) // c if u >= j: pt.append((j + 1, -c, w[j] + c * j)) pt.append((u + 1, c, -(w[j] + c * j))) u = ceildiv(c * j - w[j], c) if u <= j: pt.append((u, c, w[j] - c * j)) pt.append((j, -c, -(w[j] - c * j))) pt.append((j, 0, w[j])) pt.append((j + 1, 0, -w[j])) pt.sort() u, v = 0, 0 cur = 0 for i in range(n): while cur < len(pt) and pt[cur][0] <= i: u += pt[cur][1] v += pt[cur][2] cur += 1 if u * i + v > a[i]: return False return True l, r = -1, 10 ** 18 while r - l > 1: mid = (l + r) // 2 if check(mid): r = mid else: l = mid if r >= 10 ** 18: print(-1) exit() print(r)