Buggy python submission (verdict: Runtime Error):
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
        
print(result+sum(As[now:]))


Fixed submission:
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
        break
        
print(result+sum(As[now:]))

Buggy python submission (verdict: Runtime Error):
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if As[now+min(b-1,len(As)-1)]<=c:
        result+=c*b
        now+=b
    else:
        pos=bisect.bisect_left(As[now:],c)
        result+=c*pos
        now+=pos
        break
        
print(result+sum(As[now:]))


Fixed submission:
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
        break
        
print(result+sum(As[now:]))

Buggy python submission (verdict: Runtime Error):
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)

for b,c in BC:
    if As[b-1]<=c:
        result+=c*b
        As=As[b:]
    else:
        pos=bisect.bisect_left(As,c)
        result+=c*pos
        As=As[pos:]
        break
        
print(result+sum(As))


Fixed submission:
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
        break
        
print(result+sum(As[now:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

def i_sorted(L,num,hm):#ソート済み配列,入れる数,入れる個数
    pos=bisect.bisect_left(L,num)
    return L[:pos]+[num]*hm+L[pos:]

for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
for lst in BC:
    if As[lst[0]-1]<=lst[1]:
        As=i_sorted(As[lst[0]:],lst[1],lst[0])
    else:
        pos=bisect.bisect_left(As,lst[1])
        As=i_sorted(As[pos:],lst[1],pos)
        
print(sum(As))


Fixed submission:
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
        break
        
print(result+sum(As[now:]))

Buggy python submission (verdict: Time Limit Exceeded):
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
for lst in BC:
    if As[lst[0]-1]<=lst[1]:
        As=sorted([lst[1]]*lst[0]+As[lst[0]:])
    else:
        for i in range(lst[0]):
            if As[i]<lst[1]:
                As[i]=lst[1]
            else:
                break
        As.sort()
print(sum(As))


Fixed submission:
import bisect
N,M=(int(x) for x in input().split())
As=sorted([int(x) for x in input().split()])
BC=[]

result=0
for _ in range(M):
    BC.append([int(x) for x in input().split()])

BC=sorted(BC, key=lambda x: x[1],reverse=True)
now=0
for b,c in BC:
    if now+b-1<=len(As)-1:
        if As[now+b-1]<=c:
            result+=c*b
            now+=b
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
            break
    else:
        if As[-1]<=c:
            result+=c*(N-now)
            As=[]
        else:
            pos=bisect.bisect_left(As[now:],c)
            result+=c*pos
            now+=pos
        break
        
print(result+sum(As[now:]))