Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

N, M=map(int, input().split())
A=deque(list(map(int, input().split())))
for i in range(M):
  B, C=map(int, input().split())
  A.extend([C]*B)
  
A=sorted(list(A))
print(int(sum(A[-N:])))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
N, M=map(int, input().split())
A=list(map(int, input().split()))
for i in range(M):
  B, C=map(int, input().split())
  A=A+[C]*B
  
A.sort()
print(int(sum(A[-N:])))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
N, M=map(int, input().split())
A=list(map(int, input().split()))
for i in range(M):
  B, C=map(int, input().split())
  A=A+[C]*B
  
A.sort()
print(int(sum(A[-N:])))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M=map(int, input().split())
A=np.array(list(map(int, input().split())))
for i in range(M):
  B, C=map(int, input().split())
  A=np.r_[A, np.ones(B)*C]
  
A=np.sort(A)
print(int(np.sum(A[-N:])))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M=map(int, input().split())
A=list(map(int, input().split()))
for i in range(M):
  B, C=map(int, input().split())
  A=A+[C for i in range(B)]
  
A=np.sort(A)
print(int(np.sum(A[-N:])))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M=map(int, input().split())
A=np.sort(list(map(int, input().split())))
for i in range(M):
  B, C=map(int, input().split())
  
  n_cand=np.sum(A<C)
  if n_cand<=B:
    A[:n_cand]=C
  else:
    A=np.r_[A[B:n_cand], np.ones(B)*C, A[n_cand:]]
    
print(int(np.sum(A)))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M=map(int, input().split())
A=np.array(sorted(list(map(int, input().split()))))
for i in range(M):
  B, C=map(int, input().split())
  
  n_cand=np.sum(A<C)
  if n_cand<=B:
    A[:n_cand]=C
  else:
    A=np.r_[A[B:n_cand], np.ones(B)*C, A[n_cand:]]
    
print(int(np.sum(A)))

Fixed submission:
from collections import defaultdict

cnt=defaultdict(int)

N, M=map(int, input().split())
A=list(map(int, input().split()))

for i in range(N):
  cnt[A[i]]+=1

tmp=[0]*M
for i in range(M):
  B, C=map(int, input().split())
  cnt[C]+=B
  tmp[i]=C
A+=tmp

tmp=list(cnt.keys())
tmp.sort()
j=-1
out=0
while N>0:
  if cnt[tmp[j]]<=N:
    out+=tmp[j]*cnt[tmp[j]]
    N-=cnt[tmp[j]]
  else:
    out+=tmp[j]*N
    N=0
    
  j-=1
print(out)