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

for i in range(M):
    B,C = map(int,input().split())
    
    if A[-1] <= C:  j,m = B,N
    elif A[0] >= C: j,m = 0,0
    else:
        m,s,e = N//2, 0, N
        while A[m] > C or C > A[m+1]:
            if A[m] > C:    e = m
            elif C > A[m+1]:    s = m+1
            m = (s+e)//2
        
        if B < m:   j = B
        else:   j = m 

    A = A[j:m+1] + [C]*j + A[m+1:N]
print(sum(A))
    

Fixed submission:
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])
An = 0
C = []

for i in range(M):
    C += [[int(x) for x in input().split()]]
    
C = sorted(C,key=lambda x: x[1])[::-1]

m = 0
for i in range(M):
    b = C[i][0]
    if m == N:
        break
    if m+b >= N:
        b = N-m

    if A[m + b - 1] < C[i][1]:
        m = m + b
        An += C[i][1]*b
    else:
        c = C[i][1]
        for j in range(2,b):
            if A[m+b-j] < c:
                m = m + b - j + 1
                An += c*(b-j+1)
                break
        break

if m == N:  print(An)
else:   print(sum(A[m:]) + An)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])
A = np.array(A)

for i in range(M):
    B,C = map(int,input().split())
    
    if int(np.max(A)) <= C:  j,m = B,N
    elif int(np.min(A)) >= C: j,m = 0,0
    else:
        m = len(np.where(A <= C)[0]) - 1
        if B < m:   j = B
        else:   j = m 

    An = np.hstack((A[j:m+1],np.array([C]*j)))
    A = np.hstack((An,A[m+1:N]))

print(int(np.sum(A)))
    

Fixed submission:
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])
An = 0
C = []

for i in range(M):
    C += [[int(x) for x in input().split()]]
    
C = sorted(C,key=lambda x: x[1])[::-1]

m = 0
for i in range(M):
    b = C[i][0]
    if m == N:
        break
    if m+b >= N:
        b = N-m

    if A[m + b - 1] < C[i][1]:
        m = m + b
        An += C[i][1]*b
    else:
        c = C[i][1]
        for j in range(2,b):
            if A[m+b-j] < c:
                m = m + b - j + 1
                An += c*(b-j+1)
                break
        break

if m == N:  print(An)
else:   print(sum(A[m:]) + An)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])

for i in range(M):
    B,C = map(int,input().split())
    
    if A[-1] <= C:  j,m = B,N
    elif A[0] >= C: j,m = 0,0
    else:
        m,s,e = N//2, 0, N
        while A[m] > C or C > A[m+1]:
            if A[m] > C:    e = m
            elif C > A[m+1]:    s = m+1
            m = (s+e)//2
        
        if B < m:   j = B
        else:   j = m 

    A = A[j:m+1] + [C]*j + A[m+1:N]
A = np.array(A)
print(np.sum(A))
    

Fixed submission:
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])
An = 0
C = []

for i in range(M):
    C += [[int(x) for x in input().split()]]
    
C = sorted(C,key=lambda x: x[1])[::-1]

m = 0
for i in range(M):
    b = C[i][0]
    if m == N:
        break
    if m+b >= N:
        b = N-m

    if A[m + b - 1] < C[i][1]:
        m = m + b
        An += C[i][1]*b
    else:
        c = C[i][1]
        for j in range(2,b):
            if A[m+b-j] < c:
                m = m + b - j + 1
                An += c*(b-j+1)
                break
        break

if m == N:  print(An)
else:   print(sum(A[m:]) + An)

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

for i in range(M):
    B,C = map(int,input().split())
    
    if A[-1] <= C:  j,m = B,N
    elif A[0] >= C: j,m = 0,0
    else:
        m,s,e = N//2, 0, N
        while A[m] > C or C > A[m+1]:
            if A[m] > C:    e = m
            elif C > A[m+1]:    s = m+1
            m = (s+e)//2
        
        if B < m:   j = B
        else:   j = m 

    A = A[j:m+1] + [C]*j + A[m+1:N]

print(sum(A))
    

Fixed submission:
import numpy as np

N,M = map(int,input().split())
A = sorted([int(x) for x in input().split()])
An = 0
C = []

for i in range(M):
    C += [[int(x) for x in input().split()]]
    
C = sorted(C,key=lambda x: x[1])[::-1]

m = 0
for i in range(M):
    b = C[i][0]
    if m == N:
        break
    if m+b >= N:
        b = N-m

    if A[m + b - 1] < C[i][1]:
        m = m + b
        An += C[i][1]*b
    else:
        c = C[i][1]
        for j in range(2,b):
            if A[m+b-j] < c:
                m = m + b - j + 1
                An += c*(b-j+1)
                break
        break

if m == N:  print(An)
else:   print(sum(A[m:]) + An)