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

print(sum(sorted(A,reverse=True)[:n]))

Fixed submission:
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            A.append(c)
            A.popleft()
        else:break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

cnt=0
A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            cnt +=c
            A.popleft()
        else:break
print(cnt+sum(A))

Fixed submission:
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            A.append(c)
            A.popleft()
        else:break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

cnt,flag=0,True
A=deque(A)
for b,c in BC[::-1]:
    if not flag:break
    for i in range(b):
        if A[0]<c:
            cnt +=c
            A.popleft()
        else:
            flag=False
print(cnt+sum(A))

Fixed submission:
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            A.append(c)
            A.popleft()
        else:break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

cnt=0
for b,c in BC[::-1]:
    for i in range(b):
        if A[0]<c:
            cnt +=c
            del A[0]
        else:break
print(cnt+sum(A))

Fixed submission:
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            A.append(c)
            A.popleft()
        else:break
print(sum(A))

Buggy python submission (verdict: Runtime Error):
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

cnt=0
A=deque(A)
for b,c in BC[::-1]:
    for i in range(b):
        if A[0]<c:
            cnt +=c
            A.popleft()
        else:break
print(cnt+sum(A))

Fixed submission:
from collections import deque
n,m=map(int,input().split())
A=sorted(list(map(int,input().split())))
BC=sorted([list(map(int,input().split())) for i in range(m)],key=lambda x:x[1])

A=deque(A)
for b,c in BC[::-1]:
    if len(A)==0:break
    for i in range(b):
        if A[0]<c:
            A.append(c)
            A.popleft()
        else:break
print(sum(A))