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

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  change += [c]*b_up
  if b_up != b:
    break
#print(change)

ans = 0
for i in range(n):
  #print(ans)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

Fixed submission:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

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

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

ans = 0
for b,c in bc:
  #print(a,b,c)
  lower_c_num = 0
  for a_i in a:
    if a_i < c:
      lower_c_num += 1
      if lower_c_num > b:
        lower_c_num = b
        break
    else:
      break
    
  change = min(b, lower_c_num)
  a = a[change:]
  ans += c * change
  if len(a) == 0:
    break
  if change != b:
    break
    
ans += sum(a)

print(ans)
    

Fixed submission:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

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

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

ans = 0
for b,c in bc:
  #print(a,b,c)
  lower_c_num = 0
  for a_i in a:
    if a_i < c:
      lower_c_num += 1
  change = min(b, lower_c_num)
  a = a[change:]
  ans += c * change
  if len(a) == 0:
    break
  if change != b:
    break
    
ans += sum(a)

print(ans)
    

Fixed submission:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)

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

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

for b,c in bc:
  if a[0] > c:
    break
  for _ in range(b):
    if a[0] < c:
      del a[0]
      a.append(c)
    else:
      break
      
ans = sum(a)

print(ans)
    

Fixed submission:
from operator import itemgetter

n, m = [int(i) for i in input().split()]
a = sorted([int(i) for i in input().split()])
#print(a)

bc = []
for _ in range(m):
  bc.append([int(i) for i in input().split()])
bc = sorted(bc, key=itemgetter(1), reverse=True)
#print(bc)

change = []
for b,c in bc:
  len_change = len(change)
  b_up = b
  if n - len_change < b:
    b_up = n - len_change
  #print(b_up)
  change += [c]*b_up
  if b_up != b:
    break
change += [0]*(n-len(change))

ans = 0
for i in range(n):
  #print(ans)
  #print(i)
  if a[i] < change[i]:
    ans += change[i]
  else:
    ans += sum(a[i:])
    break
    
print(ans)