Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
DD=[0]*n
ct=0
for x,y in D:
  for i in range(y):
    if i+ct<n:
      DD[i+ct]+=x
  ct+=y
ans=0
for i in range(n):
  ans+=max(A[i],DD[i])
print(ans)


Fixed submission:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
for x in AA.keys():
  D[x]+=AA[x]
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
DD=[0]*n
ct=0
for x,y in D:
  for i in range(y):
    if i+ct<n:
      DD[i+ct]+=x
  ct+=y
ans=0
for i in range(n):
  ans+=max(A[i],DD[i])
print(ans)


Fixed submission:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
for x in AA.keys():
  D[x]+=AA[x]
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
DD=[0]*n
ct=0
for x,y in D:
  for i in range(y):
    DD[i+ct]+=x
  ct+=y
ans=0
for i in range(n):
  ans+=max(A[i],DD[i])
print(ans)

Fixed submission:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
for x in AA.keys():
  D[x]+=AA[x]
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort()
D=dict()
for i in range(m):
  b,c=map(int,input().split())
  D[c]=b
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
DD=[0]*n
ct=0
for x,y in D:
  for i in range(y):
    DD[i+ct]+=x
  ct+=y
ans=0
for i in range(n):
  ans+=max(A[i],DD[i])
print(ans)


Fixed submission:
from collections import defaultdict
from collections import Counter
n,m=map(int,input().split())
A=[int(i) for i in input().split()]
AA=dict(Counter(A))
D=defaultdict(int)
for i in range(m):
  b,c=map(int,input().split())
  D[c]+=b
for x in AA.keys():
  D[x]+=AA[x]
D=sorted(D.items(),key=lambda x:x[0],reverse=True)
ans,tot=0,0
for x,y in D:
  if tot+y<=n:
    ans+=x*y
    tot+=y
  else:
    ans+=x*(n-tot)
    tot+=n-tot
  if tot==n:
    break
print(ans)