Buggy python submission (verdict: Runtime Error):
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
d=[]
for i in range(m):
	b,c=map(int,input().split())
	d+=[c]*b
d=sorted(d)[::-1]
i=j=0
while a[i]<d[j]:
	if a[i]<d[j]:
		a[i]=d[j]
		i+=1
		j+=1
		if len(a)<=i or len(d)<=j:
			break
	else:break
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
d=[]
for i in range(m):
	b,c=map(int,input().split())
	d+=[c]*b
d=sorted(d)[::-1]
i=j=0
while a[i]<d[j]:
	if a[i]<d[j]:
		a[i]=d[j]
		i+=1
		j+=1
		if len(a)<=i or len(d)<=j:
			break
	else:break
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

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)
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

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)
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
mx=0
for i in range(m):
    b,c=map(int,input().split())
    j=0
    tmp=0
    while a[j]<c and j+1<=b:
        a[j]=c
        j+=1
        if j>=n:break
    a.sort()        
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=sorted(map(int,input().split()))
mx=0
for i in range(m):
    b,c=map(int,input().split())
    j=0
    tmp=0
    while a[j]<c and j+1<=b:
        a[j]=c
        j+=1
        if j>=n:break
    a.sort()        
print(sum(a))

Fixed submission:
# 2019/07/20

from heapq import heappop,heappush,heapify

n,m=map(int,input().split())
a=list(map(int,input().split()))
heapify(a)
bc=[]
for i in range(m):
    b,c=map(int,input().split())
    hq=(-c,b)
    heappush(bc,hq)



def cg(res):

    while bc:
        c,b=heappop(bc)
        c=-c
        while b and a:
            b-=1
            v=heappop(a)
            if c>v:res+=c
            else:
                res+=v
                return res
    return res
res=0
res=cg(res)
print(sum(a)+res)