Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        
    if _num == N:
        break
    
print(_sum)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
array = []
for _ in range(M):
    B, C = map(int, input().split())
    for __ in range(B):
        A.append(C)
        
A.sort(reverse=True)
print(sum(A[:N]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
    
array.sort(key=lambda x: x[1], reverse=True)

for i in range(M):
    A.sort()
    if A[0] < array[i][1]:
        j = 0
        while j < array[i][0]:
            if A[j] < array[i][1]:
                A[j] = array[i][1]
                j += 1
            else:
                break
    else:
        break
    
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(M):
    A.sort()
    B, C = map(int, input().split())
    i = 0
    while i < B:
        if A[i] < C:
            A[i] = C
            i += 1
        else:
            break
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(M):
    B, C = map(int, input().split())
    
    while B > 0:
        ind = A.index(min(A))
        if A[ind] < C:
            A[ind] = C
        
        B -= 1
        
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
array = []
for _ in range(M):
    tmp = list(map(int, input().split()))
    array.append(tmp)
array.sort(key=lambda x: x[1], reverse=True)

_sum = 0
_num = 0

for i in range(N+M):
    if A[0] > array[0][1]:
        _sum += A.pop(0)
        _num += 1
        
    else:
        a = array[0][0] if array[0][0] < N - _num else N - _num
        _sum += array.pop(0)[1] * a
        _num += a
        if not array:
            break
        
    if _num == N:
        break
    
if _num != N:
    while _num < N:
        _sum += A.pop(0)
        _num += 1
        
print(_sum)