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

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


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

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

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


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
import bisect
i=0
for i in range(m):
    if bc[i][1]>a[0]:
        k=min(bc[i][0],bisect.bisect_left(a,bc[i][1]))
        a[:k]=[bc[i][1]]*k
        a.sort()
    else:
        break
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)
import bisect
i=0
for i in range(m):
    if bc[i][1]>a[0]:
        k=min(bc[i][0],bisect.bisect_left(a,bc[i][1]))
        a[:k]=[bc[i][1]]*k
        a.sort()
    else:
        break
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    bc.append([b,c])
bc.sort(key=lambda x:x[1],reverse=True)
x=[0]*(m+3)
for i in range(m):
    x[i+1]=min(bisect.bisect_left(a,bc[i][1]), bc[i][0])+x[i]
    for j in range(x[i],x[i+1]+1):
        a[j]=bc[i][1]


print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    bc.append([b,c])
bc.sort(key=lambda x:x[1],reverse=True)
x=[0]*(m+3)
for i in range(m):
    x[i+1]=min(bisect.bisect_left(a,bc[i][1]), bc[i][0])+x[i]
    for j in range(x[i],x[i+1]):
        a[j]=bc[i][1]


print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    bc.append([b,c])
bc.sort(key=lambda x:x[1],reverse=True)
x=[0]*n
for i in range(m):
    x[i+1]=min(bisect.bisect_left(a,bc[i][1]), bc[i][0])+x[i]
    for j in range(x[i],x[i+1]):
        a[j]=bc[i][1]
    

print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    bc.append([b,c])
bc.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
    for j in range(min(bisect.bisect_left(a,bc[i][1]), bc[i][0])):
        a[j]=bc[i][1]
    a.sort()

print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
l=[]
for i in range(m):
    b,c=map(int,input().split())
    l.append((b,c))
l.sort(key=lambda x:x[1],reverse=True)
x=[]
for i in range(m):
    xx=[l[i][1]]*l[i][0]
    x.extend(xx)
c=0
while a[c]<x[c] :
    ans+=x[c]-a[c]
    c+=1
    if c>=len(a) or c>=len(x):
        break
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
l=[]
for i in range(m):
    b,c=map(int,input().split())
    l.append((b,c))
l.sort(key=lambda x:x[1],reverse=True)
x=[]
for i in range(m):
    xx=[l[i][1]]*l[i][0]
    x.extend(xx)
c=0
while a[c]<x[c] :
    ans+=x[c]-a[c]
    c+=1
    if c>=len(a):
        break
print(ans)


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
l=[]
for i in range(m):
    b,c=map(int,input().split())
    l.append((b,c))
l.sort(key=lambda x:x[1],reverse=True)
x=[]
for i in range(m):
    xx=[l[i][1]]*l[i][0]
    x.extend(xx)
c=0
while a[c]<x[c]:
    ans+=x[c]-a[c]
    c+=1
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
ans=sum(a)
l=[]
for i in range(m):
    b,c=map(int,input().split())
    l.append((b,c))
l.sort(key=lambda x:x[1],reverse=True)
x=[]
for i in range(m):
    xx=[l[i][1]]*l[i][0]
    x.extend(xx)
c=0
while a[c]<x[c]:
    ans+=x[c]-a[c]
    c+=1
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
for i in range(m):
    b,c=map(int,input().split())
    for i in range(b):
        if a[i]>=c:
            break
        a[i]=c
    a.sort()
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
for i in range(m):
    b,c=map(int,input().split())
    for i in range(b):
        if a[i]>=c:
            break
        a[i]=c
    a.sort()
print(sum(a))



Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
for i in range(m):
    b,c=map(int,input().split())
    for i in range(b):
        if a[0]>=c:
            break
        a[0]=c
        a.sort()
print(sum(a))




Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    l.append([1,a[i]])
for i in range(m):
    l.append(list(map(int,input().split())))
l.sort(key=lambda x:x[1], reverse=True)
c=0
ans=0
for i in range(n+m):
    ans+=l[i][1]*l[i][0]
    c+=l[i][0]
    if c>n:
        ans-=l[i][1]*(c-n)
        break
print(ans)