Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
n,m=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
a.sort
for i in range(m):
    b,c=map(int,sys.stdin.readline().split())
    t=[c]*b
    a=list(heapq.merge(a,t))
print(sum(heapq.nlargest(n,a)))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
n,m=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
heapq.heapify(a)
for i in range(m):
    b,c=map(int,sys.stdin.readline().split())
    for j in range(b):
        if a[0]<c:
            heapq.heapreplace(a,c)
        else:
            break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
n,m=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
heapq.heapify(a)
for i in range(m):
    b,c=map(int,sys.stdin.readline().split())
    for j in range(b):
        if a[0]<c:
            heapq.heapreplace(a,c)
        else:
            break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq
n,m=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
heapq.heapify(a)
for i in range(m):
    b,c=map(int,sys.stdin.readline().split())
    for j in range(b):
        if a[0]<c:
            heapq.heappop(a)
            heapq.heappush(a,c)
        else:
            break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

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):
        if a[0]<c:
            heapq.heappop(a)
            heapq.heappush(a,c)
        else:
            break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

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+=[c]*b
    a.sort(reverse=True)
    a=a[:n]
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

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+=[c]*b
    a.sort(reverse=True)
    a=a[:n]
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n,m=map(int,sys.stdin.readline().split())
a=list(map(int,sys.stdin.readline().split()))
for i in range(m):
    b,c=map(int,sys.stdin.readline().split())
    a+=[c]*b
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))

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+=[c]*b
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1],reverse=True)
for b,c in bc:
    a.extend([c]*b)
    if len(a)>2*n:
        break
a.sort(reverse=True)
print(sum(a[:n]))