Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[]

for i in range(m):
    b,c=map(int,input().split())
    bc+=[c]*b
num=min(len(bc),n)
bc=sorted(bc,reverse=True)[:num]

ans=sum(a)
temp=sum(a)
for i in range(n):
    if num<=i:continue
    temp+=-a[i]+bc[i]
    if temp>ans:ans=temp
print(ans)

Fixed submission:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
input=sys.stdin.readline
def main():
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    heapq.heapify(a)

    for i in range(m):
        b,c=map(int,input().split())
        for j in range(b):
            temp=heapq.heappop(a)
            if temp<c:
                heapq.heappush(a,c)
            else:
                heapq.heappush(a,temp)
                break
    print(sum(a))

if __name__=='__main__':
    main()

Fixed submission:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
input=sys.stdin.readline

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapq.heapify(a)

for i in range(m):
    b,c=map(int,input().split())
    for j in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            break
print(sum(a))

Fixed submission:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
input=sys.stdin.readline

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapq.heapify(a)

for i in range(m):
    b,c=map(int,input().split())
    for j in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            break
print(sum(a))

Fixed submission:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
n,m=map(int,input().split())
a=list(map(int,input().split()))
heapq.heapify(a)

for i in range(m):
    b,c=map(int,input().split())
    for j in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
print(sum(a))

Fixed submission:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
a=sorted(list(map(int,input().split())))
bc=[list(map(int,input().split())) for i in range(m)]
bc=sorted(bc,reverse=True,key=lambda x:x[1])
c=[]

cnt=0
for B,C in bc:
    for i in range(B):
        if cnt==n:break
        cnt+=1
        c.append(C)
c=sorted(c,reverse=True)

ans=sum(a)
temp=ans
for i in range(n):
    if cnt<=i:continue
    temp+=-a[i]+c[i]
    if temp>ans:ans=temp
print(ans)