Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[0]*n
count=0
for i in checklist:
    for j in range(i[1]):
        uselist[count]=i[0]
        count+=1
        if count==n:
            break
answer=0
for i in range(n):
    if lists[i]<uselist[i]:
        lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for j in range(i[1]):
        uselist.append(i[0])
        count+=1
        if count==n:
            break
answer=0
for i in range(n):
    if lists[i]<uselist[i]:
        lists[i]=uselist[i]
    else:
        break
print(sum(lists))
      

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
for i in range(m):
    b,c=map(int,input().split())
    checklist=checklist+[c]*b
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)

numbers=min(len(lists),len(checklist))

for i in range(numbers):
  if lists[i]<=checklist[i]:
    lists[i]=checklist[i]
  else:
    break
print(sum(lists))

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
lists_A=sorted(list(map(int,input().split())))
lists_BC=[]
sumk=0
cnt=0
while cnt!=m:
    if sumk<n+1and cnt !=m: 
        k,l=map(int,input().split())
        sumk+=k
        lists_BC=lists_BC+[l]*k
        cnt+=1
        
    if sumk>=n+1 and cnt!=m:
        k,l=map(int,input().split())
        cnt+=1

A=listsBC+lists_A
Ans=sorted(A)
print(sum(Ans[-n:]))
    

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
lists_A=sorted(list(map(int,input().split())))
lists_BC=[]
sumk=0
for _ in range(m):
    while sumk<n+1:
        k,l=map(int,input().split())
        sumk+=k
        lists_BC=lists_BC+[l]*k
lists_BC=sorted(lists_BC)
listsBC=lists_BC[-min(n,sumk):]
listsBC=sorted(listsBC,reverse=True)
#listsBCの左のタプルから順にlistsAについて適用していく。
#listA内のインデックス
indexcheck=0
tupleindex=0
maxi=sum(lists_A)
for _ in range(min(n,sumk)):
    a=maxi+listsBC[_]-lists_A[_]
    if a>maxi:
        maxi=a
    else:
        continue      
print(maxi)

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
lists_A=sorted(list(map(int,input().split())))
lists_BC=[]
sumk=0
for _ in range(m):
    k,l=map(int,input().split())
    sumk+=k
    
    lists_BC=lists_BC+[l]*k
lists_BC=sorted(lists_BC)
listsBC=lists_BC[-min(n,sumk):]
listsBC=sorted(listsBC,reverse=True)

#listsBCの左のタプルから順にlistsAについて適用していく。
#listA内のインデックス
indexcheck=0
tupleindex=0
maxi=sum(lists_A)
for _ in range(min(n,sumk)):
    a=maxi+listsBC[_]-lists_A[_]
    if a>maxi:
        maxi=a
    else:
        continue
        
print(maxi)
    

Fixed submission:
n,m=map(int,input().split())
lists=list(map(int,input().split()))
checklist=[]
count=0

for i in range(m):
    b,c=map(int,input().split())
    checklist.append((c,b))
checklist=sorted(checklist,reverse=True)
lists=sorted(lists)
uselist=[]
count=0

for i in checklist:
    for _ in range(i[1]):
        uselist.append(i[0])
    count+=i[1]

    if count>=n:
        break
answer=0
n=min(n,len(uselist))
for i in range(n):
    if lists[i]<uselist[i]:
            lists[i]=uselist[i]
    else:
        break
print(sum(lists))