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)]
ans = []
A.sort()
count = 0
for b,c in BC:
    if c <= A[b]:
      break
    ans += [c]*b
    count += b
ans.extend(A)
ans = sorted(ans,reverse = True)
print(sum(ans[:N]))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(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)]
ans = []
A.sort()
count = 0
for b,c in BC:
    ans += [c]*b
ans.extend(A)
ans = sorted(ans,reverse = True)
print(sum(ans[:N]))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(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)]
ans = [0]*(10**10)
A.sort()
count = 0
for b,c in BC:
    ans[count:b] = [c]*b
    count += b
ans.extend(A)
ans = sorted(ans,reverse = True)
print(sum(ans[:N]))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(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)]
ans = [0]*(N*M)
A.sort()
count = 0
for b,c in BC:
    ans[count:b] = [c]*b
    count += b
ans.extend(A)
ans = sorted(ans,reverse = True)
print(sum(ans[:N]))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(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)]

for b,c in BC:
  for _ in range(b):
    A.append(c)
A = sorted(A,reverse = True)

print(sum(A[:N]))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left,bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key=lambda x: x[1],reverse = True)
for b,c in BC:
  A.sort()
  i = bisect_left(A,c)
  if c <= A[0]:
    break
  if b < i:
    A[:b] = [c]*b
  else:
    A[:i] = [c]*i
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left,bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
for b,c in BC:
  A.sort()
  i = bisect_left(A,c)
  if c <= A[0]:
    continue
  if b < i:
    A[:b] = [c]*b
  else:
    A[:i] = [c]*i
print(sum(A))

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [(A[i], 1) for i in range(N)]
for _ in range(M):
    B, C = map(int, input().split())
    BC.append((C,B))
BC = sorted(BC,reverse = True)
ans = 0
a = 0
count = N
for c,b in BC:
  lamb = min(count,b)
  ans += c * lamb
  count -= lamb

print(ans)