Buggy python submission (verdict: Runtime Error):
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
new=[]
cards.sort()
mod=0
for i in range(M):
    time,val=list(map(int,input().split()))
    mod+=time
    tmp=[val for _ in range(time)]
    new.append([val,time])
new.sort(key=lambda x:x[0],reverse=True)
print(sum(new))
"""
total=0
vali=0
if mod<N:
    total+=sum(cards[mod-N:])
for i in new:
    for j in range(i[1]):
        if vali==N:
            break
        total+=max(i[0],cards[vali])
        vali+=1
        
print(total)
"""

Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
new=[]
cards.sort()
mod=0
for i in range(M):
    time,val=list(map(int,input().split()))
    mod+=time
    tmp=[val for _ in range(time)]
    new.append([val,time])
new.sort(key=lambda x:x[0],reverse=True)
#print(new)
total=0
vali=0
if mod<N:
    total+=sum(cards[mod-N:])
for i in new:
    for j in range(i[1]):
        if vali==N:
            break
        total+=max(i[0],cards[vali])
        vali+=1
        
print(total)


Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
new=[]
cards.sort()
mod=N
for i in range(M):
    time,val=list(map(int,input().split()))
    mod-=time
    tmp=[val for _ in range(time)]
    new.extend(tmp)
new.sort()
new=new[::-1]
total=0
if mod>0:
    total+=sum(cards[-mod:])
for i,j in zip(cards,new):
    total+=max(i,j)
print(total)

Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
for i in range(M):
    time,val=list(map(int,input().split()))
    tmp=[val for _ in range(time)]
    cards.extend(tmp)
cards=sorted(cards)[::-1]
print(sum(cards[:N]))



Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
for _ in range(M):
    tmp=list(map(int,input().split()))
    A.extend([tmp[1]]*tmp[0])
A.sort()
print(sum(A[-N:]))

Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
for _ in range(M):
    tmp=list(map(int,input().split()))
    for _ in range(tmp[0]):
        A.append(tmp[1])
A.sort()
print(sum(A[-N:]))

Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=list(map(int,input().split()))
A=list(map(int,input().split()))
#ans=[0 for i in range(max(A))]
#sortA=A[:]
#sortA.sort()
dic={}
for _ in range(M):
    tmp=list(map(int,input().split()))
    try:
        dic[tmp[1]]+=tmp[0]
    except:
        dic[tmp[1]]=tmp[0]
tmp=list(dic.keys())[:]
tmp.sort()
for each in tmp:
    b_A=[i for i in A if i>=each]
    s_A=[i for i in A if i<each]
    s_A.sort()
    num=min(dic[each],len(s_A))
    for i in range(num):
        s_A[i]=each
    A=s_A+b_A
    #print(A)
print(sum(A))

Fixed submission:
from collections import Counter
N,M=list(map(int,input().split()))
cards=list(map(int,input().split()))
all_c=Counter(cards)

for i in range(M):
    time,val=list(map(int,input().split()))
    try:
        all_c[val]+=time
    except:
        all_c[val]=time

tmp=list(all_c.keys())
tmp.sort(reverse=True)
total=0
sum_time=0
for i in tmp:
    if sum_time>=N:
        break
    sum_time+=all_c[i]
    if sum_time<N:
        total+=i*all_c[i]
    else:
        total+=i*(all_c[i]+N-sum_time)
print(total)