import sys def main(): n, m = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) # a[0] is a_1 in problem (1-based) sum_all = 0 sum_x = [0] * (n + 2) # 1-based indexing for sections people = [] for _ in range(m): x, w = map(int, sys.stdin.readline().split()) sum_all += w sum_x[x] += w people.append((x, w)) # Check if c=0 is possible valid = True for ai in a: if ai < sum_all: valid = False break if valid: print(0) return # Check if sum_x[i] exceeds a[i-1] for any i for i in range(1, n + 1): if sum_x[i] > a[i - 1]: print(-1) return # Binary search setup max_w = max(w for x, w in people) low = 1 high = max_w answer = -1 # This will be updated since sum_x is valid def is_feasible(c): diff_coeff = [0] * (n + 2) diff_const = [0] * (n + 2) for x, w in people: if c == 0: continue # Handled earlier k = (w - 1) // c # Left interval [x - k, x - 1] L_left = max(1, x - k) R_left = x - 1 if L_left <= R_left: delta_a = c delta_b = w - c * x diff_coeff[L_left] += delta_a if R_left + 1 <= n: diff_coeff[R_left + 1] -= delta_a diff_const[L_left] += delta_b if R_left + 1 <= n: diff_const[R_left + 1] -= delta_b # Right interval [x, x + k] R_right = min(n, x + k) if x <= R_right: delta_a = -c delta_b = w + c * x diff_coeff[x] += delta_a if R_right + 1 <= n: diff_coeff[R_right + 1] -= delta_a diff_const[x] += delta_b if R_right + 1 <= n: diff_const[R_right + 1] -= delta_b # Compute prefix sums for coefficients and constants coeff = [0] * (n + 2) current = 0 for i in range(1, n + 1): current += diff_coeff[i] coeff[i] = current const = [0] * (n + 2) current = 0 for i in range(1, n + 1): current += diff_const[i] const[i] = current # Check each section for i in range(1, n + 1): load = coeff[i] * i + const[i] if load > a[i - 1]: return False return True # Perform binary search while low <= high: mid = (low + high) // 2 if is_feasible(mid): answer = mid high = mid - 1 else: low = mid + 1 print(answer) if __name__ == "__main__": main()