def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 M = int(input[ptr]) ptr += 1 a = list(map(int, input[ptr:ptr+N])) ptr += N people = [] for _ in range(M): x = int(input[ptr]) w = int(input[ptr+1]) people.append((x, w)) ptr += 2 sum_w = sum(w for x, w in people) min_a = min(a) if sum_w <= min_a: print(0) return sum_xj = [0] * (N + 2) # 1-based for x, w in people: sum_xj[x] += w possible = True for i in range(1, N + 1): if sum_xj[i] > a[i - 1]: possible = False break if not possible: print(-1) return max_w = max(w for x, w in people) low = 1 high = max_w ans = -1 while low <= high: mid = (low + high) // 2 delta_S1 = [0] * (N + 2) coeff_delta = [0] * (N + 2) const_delta = [0] * (N + 2) for x, w in people: k = w // mid L = max(1, x - k) R = min(N, x + k) delta_S1[L] += w delta_S1[R + 1] -= w # Left interval [L, x] a_left = L b_left = x if a_left <= b_left: coeff_delta[a_left] -= 1 coeff_delta[b_left + 1] += 1 const_delta[a_left] += x const_delta[b_left + 1] -= x # Right interval [x, R] a_right = x b_right = R if a_right <= b_right: coeff_delta[a_right] += 1 coeff_delta[b_right + 1] -= 1 const_delta[a_right] -= x const_delta[b_right + 1] += x # Compute S1 S1 = [0] * (N + 1) current_S1 = 0 for i in range(1, N + 1): current_S1 += delta_S1[i] S1[i] = current_S1 # Check feasibility feasible = True current_coeff = 0 current_const = 0 for i in range(1, N + 1): current_coeff += coeff_delta[i] current_const += const_delta[i] coeff_i = current_coeff const_i = current_const S2_i = coeff_i * i + const_i load = S1[i] - mid * S2_i if load > a[i - 1]: feasible = False break if feasible: ans = mid high = mid - 1 else: low = mid + 1 print(ans if ans != -1 else -1) if __name__ == "__main__": main()