Buggy python submission (verdict: Runtime Error):
n, m  = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb = []
for i in range(m):
  b, c = map(int, input().split())
  cb.append([c,b])
cb.sort(reverse=True)

j = 0
for i in range(m):
  k = 0
  while k<cb[i][1] and a[k+j]<cb[i][0] and k+j<n:
    a[k+j] = cb[i][0]
    k += 1
  j += k
  if j == n:
    break
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n, m  = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb = []
for i in range(m):
  b, c = map(int, input().split())
  cb.append([c,b])
cb.sort(reverse=True)

j = 0
for i in range(m):
  k = 0
  while k<cb[i][1] and a[k+j]<cb[i][0] and k+j<n:
    a[k+j] = cb[i][0]
    k += 1
  j += k
  if j == n:
    break
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n, m  = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb = []
for i in range(m):
  b, c = map(int, input().split())
  cb.append((c,b))
cb.sort(reverse=True)

j = 0
for i in range(m):
  k = 0
  while k<cb[i][1] and a[k+j]<cb[i][0] and k+j<n:
    a[k+j] = cb[i][0]
    k += 1
  j += k
  if j == n:
    break
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

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(reverse=True, key=itemgetter(1))

j = 0
for i in range(m):
  k = 0
  while k<bc[i][0] and a[k+j]<bc[i][1] and k+j<n:
    a[k+j] = bc[i][1]
    k += 1
  j += k
  if j == n:
    break
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

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(reverse=True, key=itemgetter(1))
#print(bc)
j = 0
for i in range(m):
  k = 0
  while k<bc[i][0] and a[k+j]<bc[i][1] and k+j<n:
    a[k+j] = bc[i][1]
    k += 1
  j += k
  if j == n:
    break
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

n, m  = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=itemgetter(1))


for i in range(m):
  k = 0
  a.sort()
  while k<bc[i][0] and a[k]<bc[i][1]:
    a[k] = bc[i][1]
    k += 1
    
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

n, m  = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for i in range(m)]

bc = sorted(bc, key=itemgetter(1))

for i in range(m):
  a.sort()
  for j in range(bc[i][0]):
    if a[j] < bc[i][1]:
      a[j] = bc[i][1]
    else:
      continue
      
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())))


import bisect
for i in range(m):
  b, c = map(int, input().split())
  mid = bisect.bisect_left(a, c)
  t = min(mid, b)
  for j in range(t):
    a[j] = c
  a.sort()
  
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))

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

import bisect
for i in range(m):
  mid = bisect.bisect_left(a, bc[i][1])
  t = min(mid, bc[i][0])
  for j in range(t):
    a[j] = bc[i][1]
  a.sort()
  
print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
cb=[]
for i in range(m):
    b, c = map(int, input().split())
    cb.append([c,b])
cb.sort(reverse=True)
k=0
for i in range(m):
    j=0
    while j<cb[i][1] and k+j<n and a[k+j]<cb[i][0]:
        a[k+j] = cb[i][0]
        j+=1
    k+=j
    if k==n:
        break
print(sum(a))