Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x:x[1])
i = 0
ans = 0
for b, c in bc:
    for _ in range(b):
        if a[i] < c:
            ans += c 
            i += 1
            if i > n-1: break
        else: break
print(ans + sum(a[i:]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if n >  N-1: break
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))

Buggy python submission (verdict: Runtime Error):
n,m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

a.sort()
bc.sort(reverse=True, key=lambda x:x[1])
i = 0
ans = 0
for b, c in bc:
    for _ in range(b):
        if a[i] < c:
            ans += c
            i += 1
        else: break
print(ans + sum(a[i:]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if n >  N-1: break
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if n >  N-1: break
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

n = 0
for i in range(M):
  b,c = BC[i][0], BC[i][1]
  for j in range(b):
    if n >= N: break
    if n == 0:
      if A[n] < c: 
        A[n] = c
        n += 1
    else:
      if A[n] < c: 
        A[n] = c
        n += 1
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if n >  N-1: break
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

for b,c in BC:
  for i in range(b):
    if A[i] < c:
      A[i] = c
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(reverse=True, key=lambda x:x[1])
A.sort()

ans = []
for b,c in BC:
  n = len(ans)
  for i in range(b):
    if n >  N-1: break
    if A[n] < c:
      ans.append(c)
      n += 1
    else: break

print(sum(ans) + sum(A[len(ans):]))