Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
for m in range(M):
    if BC[m][0]<len(A) and A[BC[m][0]]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        A=A[BC[m][0]:]
    else:    
        r=bisect_left(A,BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            A=A[r:]
        break
ans+=sum(A)
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Runtime Error):
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
for m in range(M):
    if A[BC[m][0]]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        A=A[BC[m][0]:]
    else:    
        r=bisect_left(A,BC[m][1])
        ans=ans+BC[m][1]*r
        A=A[r:]
ans+=sum(A)
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
numans=0
for m in range(M):
    r=bisect_left(A,BC[m][1])
    if r:
        num=min(r,BC[m][0])
        ans=ans+sum(A[r:])+BC[m][1]*num
        numans+=len(A[r:])+num
        A=A[num:r]
    else:
        break
ans+=sum(A[-(N-numans):])
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=sorted(map(int, input().split()))
BC=sorted([list(map(int,input().split())) for i in range(M)],key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
numans=0
for m in range(M):
    r=bisect_left(A,BC[m][1])
    if r:
        num=min(r,BC[m][0])
        ans=ans+sum(A[r:])+BC[m][1]*num
        numans+=len(A[r:])+num
        A=A[num:r]
    else:
        break
ans+=sum(A[-(N-numans):])
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
A=sorted(map(int, input().split()))
BC=sorted([list(map(int,input().split())) for i in range(M)],key=lambda x: x[1],reverse=True)
from bisect import bisect_left
l=N
ans=0
numans=0
for m in range(M):
    r=bisect_left(A,BC[m][1])
    num=min(r,BC[m][0])
    if r:
        ans=ans+sum(A[r:])+BC[m][1]*num
        numans+=len(A[r:])+num
        A=A[num:r]
    else:
        break
ans+=sum(A[-(N-numans):])
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
A=sorted(map(int, input().split()))
BC=sorted([list(map(int,input().split())) for i in range(M)],key=lambda x: x[1],reverse=True)
from bisect import bisect_left
l=N
ans=0
numans=0
for m in range(M):
    r=bisect_left(A,BC[m][1])
    num=min(r,BC[m][0])
    if r:
        ans=ans+sum(A[r:])+BC[m][1]*num
        numans+=len(A[r:])+num
        A=A[num:r]
    else:
        break
ans+=sum(A[-(N-numans):])
print(ans)

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
A=sorted(map(int, input().split()))
BC=sorted([list(map(int,input().split())) for i in range(M)],key=lambda x: x[1],reverse=True)
from bisect import bisect_left
l=N
ans=[]
for m in range(M):
    r=bisect_left(A,BC[m][1])
    num=min(r,BC[m][0])
    if r:
        ans=ans+A[r:]+[BC[m][1]]*num
        A=A[num:r]
    else:
        break
ans+=A[-(N-len(ans)):]
print(sum(ans))

Fixed submission:
N,M=map(int, input().split())
import sys
def input(): return sys.stdin.readline().strip()
A=list(map(int, input().split()))
BC=[list(map(int,input().split())) for i in range(M)]
A.sort()
BC.sort(key=lambda x: x[1],reverse=True)
from bisect import bisect_left
ans=0
l=0
for m in range(M):
    if BC[m][0]<len(A)-l and A[BC[m][0]+l]<BC[m][1]:
        ans=ans+BC[m][1]*BC[m][0]
        l+=BC[m][0]
    else:     
        r=bisect_left(A[l:],BC[m][1])
        if r:
            ans=ans+BC[m][1]*r
            l+=r
        break
ans+=sum(A[l:])
print(ans)