Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
m = min(A)
cnt = 0
for i in range(N):
  if A[N-1-i] == m and i != N - 1:
    m = min(A[:N-1-i])
    cnt += 1
  if i == N - 1:
    cnt += 1
print(cnt)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
m = N + 1
cnt = 0
for i in range(N):
  if m > A[i]:
    m = A[i]
    cnt += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
m = min(A)
cnt = 0
for i in range(N):
  if A[N-1-i] == m:
    m = min(A[:N-1-i])
    cnt += 1
print(cnt)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
m = N + 1
cnt = 0
for i in range(N):
  if m > A[i]:
    m = A[i]
    cnt += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
m = min(A)
cnt = 0
for i in range(N):
  if A[N-1-i] == m:
    m = max(A[:N-1-i])
    cnt += 1
print(cnt)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
m = N + 1
cnt = 0
for i in range(N):
  if m > A[i]:
    m = A[i]
    cnt += 1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(N):
  if A[i] == min(A[:i+1]):
    cnt += 1
print(cnt)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
m = N + 1
cnt = 0
for i in range(N):
  if m > A[i]:
    m = A[i]
    cnt += 1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
mod = int(1e+9 + 7)
L = A[0]
p = mod - 2
X = []
while p != 0:
  X = [p%2] + X[:]
  p //= 2

def inved(x):
  y = 1
  for i in range(len(X)):
    if X[i] == 1:
      y *= x
      y %= mod
    if i != len(X) - 1:
      y *= y
      y %= mod
  return y

for i in range(N-1):
  a, b = L, A[i+1]
  while b != 0:
    a, b = b, a % b
  L //= a
  b = A[i+1] // a
  L = a * b * L
S = 0
for i in range(N):
  S += (L * inved(A[i])) % mod
  S %= mod
print(S)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
mod = int(1e+9 + 7)
L = A[0]
p = mod - 2
X = []
while p != 0:
  X = [p%2] + X[:]
  p //= 2

def inved(x):
  y = 1
  for i in range(len(X)):
    if X[i] == 1:
      y *= x
      y %= mod
    if i != len(X) - 1:
      y *= y
      y %= mod
  return y

for i in range(N-1):
  a, b = L, A[i+1]
  while b != 0:
    a, b = b, a % b
  b = A[i+1] // a
  L = b * L
S = 0
for i in range(N):
  S += (L * inved(A[i])) % mod
  S %= mod
print(S)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [list(map(str, input().split())) for i in range(N)]
S = input()
i = 0
while A[i][0] != S:
  i += 1
print(sum(int(A[j][1]) for j in range(i + 1, N))

Fixed submission:
N = int(input())
A = [list(map(str, input().split())) for i in range(N)]
S = input()
i = 0
while A[i][0] != S:
  i += 1
print(sum(int(A[j][1]) for j in range(i + 1, N)))
Buggy python submission (verdict: Runtime Error):
S, T = map(str, input.split())
A = T[:] + S[:]
print(A)

Fixed submission:
S, T = map(str, input().split())
A = T[:] + S[:]
print(A)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

mod = int(1e+9 + 7)
L = [0, 0, 0]
pi = 1
for i in range(N):
  j = 0
  while L[j] != A[i]:
    j += 1
  n = 0
  for k in range(3):
    if L[k] == A[i]:
      n += 1
  if j != 3:
    L[j] += 1
  pi *= n
  pi %= mod
print(pi)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))

mod = int(1e+9 + 7)
L = [0, 0, 0]
pi = 1
for i in range(N):
  j = 0
  while L[j] != A[i]:
    j += 1
    if j == 3:
      break
  n = 0
  for k in range(3):
    if L[k] == A[i]:
      n += 1
  if j != 3:
    L[j] += 1
  pi *= n
  pi %= mod
print(pi)
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())
mod = int(1e+9 + 7)
n = mod - 2
x, y = (2 * X - Y) // 3, (- X + 2 * Y) // 3

ax = []
while n != 0:
    ax = [n % 2] + ax[:]
    n //= 2
S = 0
if (2 * X - Y) % 3 != 0 or (-X + 2 * Y) % 3 != 0:
    S = 0
else:
    A = [1]
    for i in range(x+y):
        A.append(((i + 1) * A[i]) % mod)
    inv = [1, 1]
    for i in range(len(ax)):
        if ax[i] == 1:
            inv[0] *= A[x]
            inv[1] *= A[y]
            inv[0] %= mod
            inv[1] %= mod
        if i != len(ax) - 1:
            inv[0] *= inv[0] % mod
            inv[0] %= mod
            inv[1] *= inv[1] % mod
            inv[1] %= mod
    S = (inv[0] * inv[1] * A[x+y]) % mod
print(S)

Fixed submission:
X, Y = map(int, input().split())
mod = int(1e+9 + 7)
n = mod - 2
x, y = (2 * X - Y) // 3, (- X + 2 * Y) // 3

ax = []
while n != 0:
    ax = [n % 2] + ax[:]
    n //= 2
S = 0
if (2 * X - Y) % 3 != 0 or (-X + 2 * Y) % 3 != 0:
    S = 0
elif X > 2 * Y or Y > 2 * X:
  S = 0
else:
    A = [1]
    for i in range(x+y):
        A.append(((i + 1) * A[i]) % mod)
    inv = [1, 1]
    for i in range(len(ax)):
        if ax[i] == 1:
            inv[0] *= A[x]
            inv[1] *= A[y]
            inv[0] %= mod
            inv[1] %= mod
        if i != len(ax) - 1:
            inv[0] *= inv[0] % mod
            inv[0] %= mod
            inv[1] *= inv[1] % mod
            inv[1] %= mod
    S = (inv[0] * inv[1] * A[x+y]) % mod
print(S)
Buggy python submission (verdict: Time Limit Exceeded):
N, T = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
X.sort()
dp = [-1 for _ in range(T+X[-1][0]+10)]
dp[0] = 0
for i in range(N):
  for j in range(T, 0, -1):
    if dp[j-1] != -1:
      dp[j-1+X[i][0]] = max(dp[j-1+X[i][0]], dp[j-1] + X[i][1])
print(max(dp))

Fixed submission:
N, T = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N)]
X.sort()
dp = [-1 for _ in range(T+X[-1][0]+10)]
dp[0] = 0
for i in range(N):
  for j in range(T, 0, -1):
    if dp[j-1] != -1:
      dp[j-1+X[i][0]] = max(dp[j-1+X[i][0]], dp[j-1] + X[i][1])
print(max(dp))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
D = list(map(int, input().split()))

if D[0] != 0:
  print(0)
else:
  M = max(D)
  z = 0
  L = []
  for u in range(M+1):
    s = 0
    for v in range(1, len(D)):
      if D[v] == u:
        s += 1
    L.append(s)
  if L[0] != 0:
    print(0)
  else:
    P = 1
    for x in range(1, len(L)-1):
      P = (P * (L[x] ** L[x+1])) % 998244353
    print(P)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
M = max(A)
D = {}
for i in range(N):
  if A[i] in D:
    D[A[i]] += 1
  else:
    D[A[i]] = 1
sorted(D)
S = 1
if A[0] == 0 and D[0] == 1:
  for i in D:
    if i != M:
      if i + 1 not in D:
        S = 0
        break
      else:
        S *= (D[i] ** D[i+1]) % 998244353
        S %= 998244353
else:
  S = 0
print(S % 998244353)
Buggy python submission (verdict: Runtime Error):
import math
a, b, x = map(int, input().split())

V = (a ** 2) * b

if x <= V / 2:
  t = 2 * x / (a * (b ** 2))
elif x > V / 2:
  t = (a ** 3) / (2 * (V - x))
  
def f(x):
  return math.tan(x) - t
def g(x):
  return math.tan(x) - 1 / t
if t < 1:
  S = math.pi / 4
  for i in range(100):
    S = S - f(S) * (math.cos(S) ** 2)
  print(90 - 180 * S / math.pi)
else:
  S = math.pi / 4
  for i in range(100):
    S = S - g(S) * (math.cos(S) ** 2)
  print(180 * S / math.pi)

Fixed submission:
import math
a, b, x = map(int, input().split())

V = (a ** 2) * b

if x <= V / 2:
  t = 2 * x / (a * (b ** 2))
elif x > V / 2 and x != V:
  t = (a ** 3) / (2 * (V - x))

if x != V and t != 0:
  def f(x):
    return math.tan(x) - t
  def g(x):
    return math.tan(x) - 1 / t
  if t < 1:
    S = math.pi / 4
    for i in range(100):
      S = S - f(S) * (math.cos(S) ** 2)
    print(90 - 180 * S / math.pi)
  else:
    S = math.pi / 4
    for i in range(100):
      S = S - g(S) * (math.cos(S) ** 2)
    print(180 * S / math.pi)
elif x == V:
  print(0)
Buggy python submission (verdict: Runtime Error):
N = int(input())
d = list(map(int, input()))
S = d[0]
delic = 0
for i in range(N-1):
  P = d[i+1]
  delic += P * S
  S += P
print(delic)

Fixed submission:
N = int(input())
d = list(map(int, input().split()))
S = 0
for i in range(N - 1):
  for j in range(i+1, N):
    S += d[i] * d[j]
    
print(S)
Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split())
while B != 0:
  A, B = B, A % B
i, j = 0, 1
L = []
for k in range(2, A ** (1/2) + 1):
  z = 0
  for i in range(len(L)):
    if k % L[i] == 0:
      z += 1
      break
  if z == 0:
    L.append(k)
while i <= len(L) and A != 1:
  if A % L[i] == 0:
    j += 1
  i += 1
print(j)

Fixed submission:
A, B = map(int, input().split())
a, b = A, B
while b != 0:
  a, b = b, a % b
prod = 1
s = 1
p = 2
g = a
while a != 1:
  i = 0
  while a % p == 0:
    i += 1
    a //= p
    prod *= p
  if i != 0:
    s += 1
  p += 1
  if p > int(g**(1/2)) and prod != g:
    s += 1
    break
print(s)

Buggy python submission (verdict: Runtime Error):
A, B = map(int, input().split())
while B != 0:
  A, B = B, A % B
i, j = 0, 1
L = []
for k in range(2, A // 2 + 1):
  z = 0
  for i in range(len(L)):
    if k % L[i] == 0:
      z += 1
      break
  if z == 0:
    L.append(k)
while i <= len(L) and A != 1:
  if A % L[i] == 0:
    j += 1
    while A % L[i] == 0:
      A //= L[i]
  i += 1
print(j)

Fixed submission:
A, B = map(int, input().split())
a, b = A, B
while b != 0:
  a, b = b, a % b
prod = 1
s = 1
p = 2
g = a
while a != 1:
  i = 0
  while a % p == 0:
    i += 1
    a //= p
    prod *= p
  if i != 0:
    s += 1
  p += 1
  if p > int(g**(1/2)) and prod != g:
    s += 1
    break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
A, B = map(int, input().split())
while B != 0:
  A, B = B, A % B
i, j = 2, 1
while i <= A and A != 1:
  if A % i == 0:
    j += 1
    while A % i == 0:
      A //= i
  i += 1
print(j)

Fixed submission:
A, B = map(int, input().split())
a, b = A, B
while b != 0:
  a, b = b, a % b
prod = 1
s = 1
p = 2
g = a
while a != 1:
  i = 0
  while a % p == 0:
    i += 1
    a //= p
    prod *= p
  if i != 0:
    s += 1
  p += 1
  if p > int(g**(1/2)) and prod != g:
    s += 1
    break
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
A, B = map(int, input().split())
while B != 0:
  A, B = B, A % B
i, j = 2, 1
g = A
while i <= g and A != 1:
  if A % i == 0:
    j += 1
    while A % i == 0:
      A //= i
  i += 1
print(j)

Fixed submission:
A, B = map(int, input().split())
a, b = A, B
while b != 0:
  a, b = b, a % b
prod = 1
s = 1
p = 2
g = a
while a != 1:
  i = 0
  while a % p == 0:
    i += 1
    a //= p
    prod *= p
  if i != 0:
    s += 1
  p += 1
  if p > int(g**(1/2)) and prod != g:
    s += 1
    break
print(s)
Buggy python submission (verdict: Time Limit Exceeded):
N, K, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(int(input()))

B = []
for j in range(1, N+1):
  sj = 0
  for k in range(Q):
    if j != A[k]:
      sj += 1
    if sj >= K:
      break
  if sj < K:
    B.append('Yes')
  else:
    B.append('No')

for l in range(len(B)):
  print(B[l])

Fixed submission:
N, K, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(int(input()))
B = [0 for b in range(N)]

for j in range(len(A)):
  B[A[j]-1] += 1
  
for l in range(len(B)):
  if B[l] > Q - K:
    print('Yes')
  else:
    print('No')

Buggy python submission (verdict: Runtime Error):
N, K, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(int(input()))

B = []
for j in range(1, N+1):
  sj = 0
  for k in range(Q):
    if j != A[k]:
      sj += 1
    if sj >= K:
      break
  if sj < K:
    B.append('Yes')
  else:
    B.append('No')

for l in range(len(B)):
  print(B[i])

Fixed submission:
N, K, Q = map(int, input().split())
A = []
for i in range(Q):
  A.append(int(input()))
B = [0 for b in range(N)]

for j in range(len(A)):
  B[A[j]-1] += 1
  
for l in range(len(B)):
  if B[l] > Q - K:
    print('Yes')
  else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
Q = []
for i in range(N):
  Q.append(max(A))
  A.remove(max(A))
for j in range(M):
  Q[0] //= 2
  for k in range(1, N-1):
    if Q[0] <= Q[k] and Q[0] >= Q[k+1]:
      Q.insert(k+1, Q[0])
      Q.remove(Q[0])
    if Q[0] < Q[N-1]:
      Q.append(Q[0])
      Q.remove(Q[0])
S = 0
for k in range(N):
  S += Q[k]
print(S)

Fixed submission:
import heapq
pq = []

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in A:
  heapq.heappush(pq, -i)
for i in range(M):
  x = pq[0]
  heapq.heappop(pq)
  heapq.heappush(pq, -((-x)//2))
print(-sum(pq))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

for i in range(M):
  m = max(A)
  if m == 0:
    break
  A.remove(m)
  A.append(int((m - m % 2) * 0.5))
S = 0
for j in range(N):
  S += A[j]
print(S)

Fixed submission:
import heapq
pq = []

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in A:
  heapq.heappush(pq, -i)
for i in range(M):
  x = pq[0]
  heapq.heappop(pq)
  heapq.heappush(pq, -((-x)//2))
print(-sum(pq))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

for i in range(M):
  makks = max(A)
  if makks == 0:
    break
  A.remove(max(A))
  A.append(makks // 2)
S = 0
for j in range(len(A)):
  S += A[j]
print(S)

Fixed submission:
import heapq
pq = []

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in A:
  heapq.heappush(pq, -i)
for i in range(M):
  x = pq[0]
  heapq.heappop(pq)
  heapq.heappush(pq, -((-x)//2))
print(-sum(pq))
Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
k, l = 0, 0

while l < B:
  k += 1
  if k == 0:
    l += A
  else:
    l += A - 1
  
print(k)

Fixed submission:
A, B = map(int, input().split())
k, l = 0, 0
if B != 1:
  while l < B:
    if k == 0:
      l += A
    else:
      l += A - 1
    k += 1
  print(k)
elif B == 1:
  print(0)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
H = list(map(int, input().split()))
C = []

H.append(max(H)+1)
for s in range(N):
  i = 0
  while H[s+i] >= H[s+i+1]:
    i += 1
  C.append(i)
print(max(C))

Fixed submission:
N = int(input())
H = list(map(int, input().split()))
C = [0]

for i in range(1, len(H)):
  if H[i-1] < H[i]:
    C.append(i)
C.append(N)
    
D = [C[j+1] - C[j] - 1 for j in range(len(C)-1)]

print(max(D))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
H = list(map(int, input().split()))

q = 0
if len(H) >= 2:
  for i in range(len(H)-1):
    if max(H[:i+1]) > H[i+1] + 1:
      q += 1
    
if q == 0:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
H = list(map(int, input().split()))

q, c = 0, 0
if len(H) >= 2:
  i = 0
  while i < len(H) - 1 and q == 0:
    if c <= H[i]:
      c = H[i]
    if c > H[i+1] + 1:
      q += 1
    i += 1
    
if q == 0:
  print('Yes')
else:
  print('No')

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
H = list(map(int, input().split()))

q = 0
if len(H) >= 2:
  for i in range(len(H)-1):
    for j in range(i+1, len(H)):
      if H[i] - 1 > H[j]:
        q += 1

if q == 0:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
H = list(map(int, input().split()))

q, c = 0, 0
if len(H) >= 2:
  i = 0
  while i < len(H) - 1 and q == 0:
    if c <= H[i]:
      c = H[i]
    if c > H[i+1] + 1:
      q += 1
    i += 1
    
if q == 0:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int, input().split()))

q = 0
for i in range(len(p)):
  if A[i] != i+1:
    q += 1

if q <= 2:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
p = list(map(int, input().split()))

q = 0
for i in range(len(p)):
  if p[i] != i+1:
    q += 1

if q <= 2:
  print('YES')
else:
  print('NO')
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input()))
m, i = 0, -1
for j in range(len(A)):
  if A[j] > m:
    m, i = A[j], j
B = A[:]
B.remove(m)
M = max(B)
for k in range(len(A)):
  if k != i:
    A[k] = m
  else:
    A[k] = M
for l in range(len(A)):
  print(A[l])

Fixed submission:
N = int(input())
A = []
for c in range(N):
  A.append(int(input()))
m, i = 0, -1
for j in range(len(A)):
  if A[j] > m:
    m, i = A[j], j
B = A[:]
B.remove(m)
M = max(B)
for k in range(len(A)):
  if k != i:
    A[k] = m
  else:
    A[k] = M
for l in range(len(A)):
  print(A[l])
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
B = [0 for _ in range(N)]
for i in range(N, 0, -1):
  S = sum(B[m*(i+1)-1] for m in range(1, N//i))
  B[i-1] = (A[i-1] + S) % 2
C = []
for i in range(N):
  if B[i] == 1:
    C.append(i+1)
print(len(C))
for i in range(len(C)):
  if i != len(C) - 1:
    print(C[i], end = ' ')
  else:
    print(C[i])


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = [0 for _ in range(N)]
for i in range(N, 0, -1):
  S = sum(B[(m+1)*i-1] for m in range(1, N//i))
  B[i-1] = (A[i-1] + S) % 2
C = []
for i in range(N):
  if B[i] == 1:
    C.append(i+1)
print(len(C))
for i in range(len(C)):
  if i != len(C) - 1:
    print(C[i], end = ' ')
  else:
    print(C[i])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

def XOR(x, y):
  u, i = 0, 0
  while x != 0 or y != 0:
    u += ((x + y) % 2) * (2 ** i)
    x //= 2
    y //= 2
    i += 1
  return u

Q = []
for i in range(1, len(A)):
  M = XOR(A[0], A[i])
  L = [A[0], A[i]]
  for j in range(len(A)-2):
    L.append(XOR(L[j], L[j+1]))
  Q.append(L)

B = A[:]
k = 0
for s in range(len(Q)):
  for l in range(len(Q[s])):
    for g in range(len(A)):
      if A[g] == Q[s][l]:
        B[g] = Q[s][l]
  if B == Q[s]:
    k += 1
    
if k != 0:
  print('Yes')
else:
  print('No')

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
def xored(a, b):
  y = 0
  base = 1
  while a != 0 or b != 0:
    y += ((a + b) % 2) * base
    base *= 2
    a //= 2
    b //= 2
  return y

if max(A) == 0:
  print("Yes")
else:
  if N % 3 != 0:
    print("No")
  else:
    D = {}
    for i in A:
      if i in D:
        D[i] += 1
      else:
        D[i] = 1
    if len(D) > 3:
      print("No")
    else:
      if len(D) == 3 and min(D[i] for i in D) == max(D[i] for i in D):
        L = [i for i in D]
        if xored(L[0], L[1]) == L[2]:
          print("Yes")
        else:
          print("No")
      elif len(D) == 2:
        if 0 not in D:
          print("No")
        else:
          if D[0] >= N // 3:
            print("Yes")
          else:
            print("No")
      else:
        print("No")
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input()))
A.sort()
print(A[N//2] - A[N//2 - 1])

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
A.sort()
print(abs(A[N//2]-A[N//2-1]))
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
L, R = [0 for _ in range(N)], [0 for _ in range(N)]
L[0] = A[0]
R[N-1] = A[N-1]
for i in range(N):
  L[i+1] = L[i] + A[i+1]
  R[N-2-i] = R[N-1-i] + A[N-2-i]
m = int(1e15)
for i in range(1, N):
  a = abs(L[i-1] - R[i])
  if a < m:
    m = a
print(m)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
L, R = [0 for _ in range(N)], [0 for _ in range(N)]
L[0] = A[0]
R[N-1] = A[N-1]
for i in range(N-1):
  L[i+1] = L[i] + A[i+1]
  R[N-2-i] = R[N-1-i] + A[N-2-i]
m = int(1e15)
for i in range(1, N):
  a = abs(L[i-1] - R[i])
  if a < m:
    m = a
print(m)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
D = {}
for i in range(M):
  B, C = map(int, input().split())
  if C in D:
    D[C] += B
  else:
    D[C] = B
sorted(D, reverse=True)
S = 0
for i in range(N):
  if D != {}:
    M = max(j for j in D)
  else:
    M = 0
  if A[i] <= M:
    S += M
    D[M] -= 1
    if D[M] == 0:
      del D[M]
  else:
    S += A[i]
print(S)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
D = []
for i in range(M):
  B, C = map(int, input().split())
  D.append([C, B])
D.sort(reverse=True)
E = []
j = 0
for i in range(N):
  if j < M:
    if D[j][1] == 0:
      j += 1
    if j == M:
      E.append(0)
    else:
      E.append(D[j][0])
      D[j][1] -= 1
  else:
    E.append(0)
S = 0
for i in range(N):
  S += max(A[i], E[i])
print(S)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
S = [list(map(int, input().split())) for i in range(M)]
A.sort()
B = []
for i in range(M):
  for j in range(S[i][0]):
    B.append(S[i][1])
B.sort()
for ix in range(min(len(B), N)):
  if A[ix] < B[len(B)-1-ix]:
    A[ix] = B[len(B)-1-ix]
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
D = []
for i in range(M):
  B, C = map(int, input().split())
  D.append([C, B])
D.sort(reverse=True)
E = []
j = 0
for i in range(N):
  if j < M:
    if D[j][1] == 0:
      j += 1
    if j == M:
      E.append(0)
    else:
      E.append(D[j][0])
      D[j][1] -= 1
  else:
    E.append(0)
S = 0
for i in range(N):
  S += max(A[i], E[i])
print(S)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
case = []
for i in range(M):
  case.append(list(map(int, input().split())))

B = A[:]
C = []
for i in range(M):
  for j in range(case[i][0]):
    C.append(case[i][1])

while min(B) < max(C):
  B.append(max(C))
  B.remove(min(B))
  C.remove(max(C))
  if len(B) == 0 or len(C) == 0:
    break
  
S = 0
for i in range(len(B)):
  S += B[i]

print(S)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
D = []
for i in range(M):
  B, C = map(int, input().split())
  D.append([C, B])
D.sort(reverse=True)
E = []
j = 0
for i in range(N):
  if j < M:
    if D[j][1] == 0:
      j += 1
    if j == M:
      E.append(0)
    else:
      E.append(D[j][0])
      D[j][1] -= 1
  else:
    E.append(0)
S = 0
for i in range(N):
  S += max(A[i], E[i])
print(S)