Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    if a >= c or i == len(As):
      break
    else:
      num += c
      i += 1
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    if a < c:
      num += c
      i += 1
    else:
      break
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

As = [int(x) for x in input().split(' ')]
As.sort()

Bs = []
Cs = []
for _ in range(M):
  b, c = [int(x) for x in input().split(' ')]
  Cs += [c for _ in range(b)]
if len(Cs) > N:
  Cs = Cs.sort(reverse=True)[: N+1]
else:
  Cs.sort()
  
num = 0
for i in range(len(Cs)):
  a = As[i]
  c = Cs[i]
  if a < c:
    num += c
  else:
    break
num += sum(As[i+1: ])
    
print(num)

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

As = [int(x) for x in input().split(' ')]
As.sort()

Bs = []
Cs = []
for _ in range(M):
  b, c = [int(x) for x in input().split(' ')]
  Cs += [c for _ in range(b)]
if len(Cs) > N:
  Cs = Cs.sort(reverse=True)[: N+1]
else:
  Cs.sort()
  
num = 0
for i in range(len(Cs)):
  a = As[i]
  c = Cs[i]
  if a < c:
    num += c
  else:
    break
num += sum(As[i+1: ])
    
print(num)

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = [int(x) for x in input().split(' ')]

As = [int(x) for x in input().split(' ')]
As.sort()

Bs = []
Cs = []
for _ in range(M):
  b, c = [int(x) for x in input().split(' ')]
  Cs += [c for _ in range(b)]
if len(Cs) > N:
  Cs = Cs.sort(reverse=True)[: N+1]
else:
  Cs.sort()
  
num = 0
for i in range(len(Cs)):
  a = As[i]
  c = Cs[i]
  if a < c:
    num += c
  else:
    break
num += sum(As[i+1: ])
    
print(num)

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = [int(x) for x in input().split(' ')]

As = [int(x) for x in input().split(' ')]
As.sort()

for _ in range(M):
  b, c = [int(x) for x in input().split(' ')]
  for i in range(b):
    if As[i] <= c:
      As[i] = c
    else:
      break
  As.sort()
  
print(sum(As))

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)

Buggy python submission (verdict: Runtime Error):
N, M = [int(x) for x in input().split(' ')]

as = [int(x) for x in input().split(' ')]
as.sort()

for _ in range(M):
  b, c = [int(x) for x in input().split(' ')]
  for i in range(b):
    if a[i] <= c:
      a[i] = c
    else:
      break
  as.sort()
  
print(sum(as))

Fixed submission:
N, M = list(map(int, input().split()))

As = list(map(int, input().split()))
As.sort()

BCs = []
for _ in range(M):
  b, c = tuple(map(int, input().split()))
  BCs.append((b, c))
BCs.sort(key=lambda x: x[1], reverse=True)
  
num = 0
i = 0
for b, c in BCs:
  for j in range(b):
    a = As[i]
    
    if a >= c:
      break
    else:
      num += c
      i += 1
      
    if i == len(As):
      break
      
  if a >= c or i == len(As):
    break
num += sum(As[i: ])
    
print(num)