Buggy python submission (verdict: Runtime Error):
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=[list(map(int,input().split())) for i in range(s[1])]
a=0
for i in range(s[1]):
  for j in range(u[i][0]):
    t.append(u[i][1])
t.sort()
for l in range(s[0]):
  a = a+t[len(t)-l-1]
print(a)


Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=[list(map(int,input().split())) for i in range(s[1])]
a=0
b=0
for i in range(s[1]):
  b=b+u[i][0]
  for j in range(u[i][0]):
    t.append(u[i][1])
t.sort()
for l in range(s[0]):
  a = a+t[s[0]+b-1-l]
print(a)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=[list(map(int,input().split())) for i in range(s[1])]
a=0
for i in range(s[1]):
  for j in range(u[i][0]):
    t.append(u[i][1])
t.sort()
t.reverse()
for l in range(s[0]):
  a = a+t[l]
print(a)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=[list(map(int,input().split())) for i in range(s[1])]
t.sort()
a=0
for i in range(s[1]):
  for j in range(u[i][0]):
    if t[j]<u[i][1]:
      t[j]=u[i][1]
  t.sort()

for pq in range(s[0]):
  a=a+t[pq]
print(a)

Fixed submission:
N,M=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
B=[list(map(int,input().split())) for _ in range(M)]
B.sort(key=lambda x:-x[1])
i,j=0,0
while i<=N-1:
  for l in range(B[j][0]):
    if A[i]>=B[j][1]:
      print(sum(A))
      exit()
    A[i]=B[j][1]
    i+=1
    if i==N:
      print(sum(A))
      exit()
  j+=1
  if j==M:
    print(sum(A))
    exit()
print(sum(A))