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 i in range(m)]
a.sort()
bc.sort(key=lambda x: x[1],reverse=True)
# print(a)
# print(bc)
i=0
for b,c in bc:
  tb=b
  while tb>0:
    if a[i]>=c:
      break
    a[i]=c
    tb-=1
    i+=1
  if a[i]>=c or n==i:
    break
print(sum(a))


Fixed submission:
# ABC127 D-Integer Cards
from operator import itemgetter
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(key=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print(sum(A))
            exit()
        c += 1
        if c == N:
            print(sum(A))
            exit()
# for i in range(M):
#     x = 0
#     while x < BC[i][0]:
#         A.sort()
#         print(i,A)
#         if BC[i][1] > A[0]:
#             A[0] = BC[i][1]
#         x += 1 
print(sum(A))

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 i in range(m)]
a.sort()
bc.sort(key=lambda x: x[1],reverse=True)
# print(a)
# print(bc)
i=0
for b,c in bc:
  tb=b
  while tb>0:
    if a[i]>=c:
      break
    a[i]=c
    tb-=1
    i+=1
  if a[i]>=c:
    break
print(sum(a))

Fixed submission:
# ABC127 D-Integer Cards
from operator import itemgetter
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(key=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print(sum(A))
            exit()
        c += 1
        if c == N:
            print(sum(A))
            exit()
# for i in range(M):
#     x = 0
#     while x < BC[i][0]:
#         A.sort()
#         print(i,A)
#         if BC[i][1] > A[0]:
#             A[0] = BC[i][1]
#         x += 1 
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
# ABC127 D-Integer Cards
from operator import itemgetter
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=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
for i in range(M):
    x = 0
    while x < BC[i][0]:
        A.sort()
        # print(i,A)
        if BC[i][1] > A[0]:
            A[0] = BC[i][1]
        x += 1 
print(sum(A))

Fixed submission:
# ABC127 D-Integer Cards
from operator import itemgetter
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(key=itemgetter(1),reverse=True)
# print(A)
# print('sort',BC)
c = 0
for i in range(M):
    for j in range(BC[i][0]):
        if A[c] < BC[i][1]:
            A[c] = BC[i][1]
        else:
            print(sum(A))
            exit()
        c += 1
        if c == N:
            print(sum(A))
            exit()
# for i in range(M):
#     x = 0
#     while x < BC[i][0]:
#         A.sort()
#         print(i,A)
#         if BC[i][1] > A[0]:
#             A[0] = BC[i][1]
#         x += 1 
print(sum(A))