Buggy python submission (verdict: Time Limit Exceeded):
import bisect as bi
N,M=map(int,input().split())
A=sorted(list(map(int,input().split())))
L=[]
for i in range(M):
    l=list(map(int,input().split()))
    L.append(l)
L=sorted(L,reverse=True,key=lambda x: x[1])
x=0
for i in range(M):
    if bi.bisect_left(A,L[i][1])==0:
        break
    if len(A)==0:
        break
    if len(A)>=L[i][0]:
        if A[L[i][0]-1]<=L[i][1]:
            m=L[i][0]
            del A[0:m]
            x+=m*L[i][1]
        else:
            m=bi.bisect_left(A,L[i][1])
            del A[0:m]
            x+=m*L[i][1]
    else:
        m=bi.bisect_left(A,L[i][1])
        del A[0:m]
        x+=m*L[i][1]
print(x+sum(A))




Fixed submission:
from operator import itemgetter
l = list(map(int, input().split()))
N = l[0]
M = l[1]
A = list(map(int, input().split()))
B = []
for i in range(M):
    b = list(map(int, input().split()))
    B.append(b)
A = sorted(A)
B = sorted(B, key=lambda x: x[1])
m = 0
s = 0
for i in range(M):
    i = M-1-i
    if B[i][0]+m <= N-1:

        if A[B[i][0]+m] <= B[i][1]:
            m = m+B[i][0]
            s += B[i][1]*B[i][0]
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
    else:
        if A[N-1] <= B[i][1]:
            s = s+B[i][1]*(N-m)
            m = N
            break
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
if m < N:
    for i in range(m, N):
        s += A[i]
print(s)

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




Fixed submission:
from operator import itemgetter
l = list(map(int, input().split()))
N = l[0]
M = l[1]
A = list(map(int, input().split()))
B = []
for i in range(M):
    b = list(map(int, input().split()))
    B.append(b)
A = sorted(A)
B = sorted(B, key=lambda x: x[1])
m = 0
s = 0
for i in range(M):
    i = M-1-i
    if B[i][0]+m <= N-1:

        if A[B[i][0]+m] <= B[i][1]:
            m = m+B[i][0]
            s += B[i][1]*B[i][0]
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
    else:
        if A[N-1] <= B[i][1]:
            s = s+B[i][1]*(N-m)
            m = N
            break
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
if m < N:
    for i in range(m, N):
        s += A[i]
print(s)

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




Fixed submission:
from operator import itemgetter
l = list(map(int, input().split()))
N = l[0]
M = l[1]
A = list(map(int, input().split()))
B = []
for i in range(M):
    b = list(map(int, input().split()))
    B.append(b)
A = sorted(A)
B = sorted(B, key=lambda x: x[1])
m = 0
s = 0
for i in range(M):
    i = M-1-i
    if B[i][0]+m <= N-1:

        if A[B[i][0]+m] <= B[i][1]:
            m = m+B[i][0]
            s += B[i][1]*B[i][0]
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
    else:
        if A[N-1] <= B[i][1]:
            s = s+B[i][1]*(N-m)
            m = N
            break
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
if m < N:
    for i in range(m, N):
        s += A[i]
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect as bi
N,M=map(int,input().split())
A=list(map(int,input().split()))
L=[]
for i in range(M):
    l=list(map(int,input().split()))
    L.append(l)
L=sorted(L,reverse=True,key=lambda x: x[1])
A=sorted(A)
x=[]
for i in range(M):
    if bi.bisect_left(A,L[i][1])==0:
        break
    if len(A)==0:
        break
    m=min(L[i][0],bi.bisect_left(A,L[i][1]))
    for l in range(m):
        A.pop(0)
        x.append(L[i][1])
print(sum(x)+sum(A))




Fixed submission:
from operator import itemgetter
l = list(map(int, input().split()))
N = l[0]
M = l[1]
A = list(map(int, input().split()))
B = []
for i in range(M):
    b = list(map(int, input().split()))
    B.append(b)
A = sorted(A)
B = sorted(B, key=lambda x: x[1])
m = 0
s = 0
for i in range(M):
    i = M-1-i
    if B[i][0]+m <= N-1:

        if A[B[i][0]+m] <= B[i][1]:
            m = m+B[i][0]
            s += B[i][1]*B[i][0]
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
    else:
        if A[N-1] <= B[i][1]:
            s = s+B[i][1]*(N-m)
            m = N
            break
        else:
            if A[m] <= B[i][1]:
                for l in range(m, N):
                    if A[l] >= B[i][1]:
                        s += B[i][1]*(l-m)
                        m = l
                        break
            else:
                break
if m < N:
    for i in range(m, N):
        s += A[i]
print(s)