Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

import bisect
A.sort()
for i in range(M):
    b, c = map(int, input().split())
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    if idx > b:
        A = A[b:idx] + [c] * b + A[idx:]
    else:
        #print("a", b, idx)
        A = [c] * idx + A[idx:]
    #print(A)
print(sum(A))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

import bisect
A.sort()
A = [-1] + A
for i in range(M):
    b, c = map(int, input().split())
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    A = A[:idx] + [c] * b + A[idx:]
    A = A[-(N+1):]
    #print(A)
print(sum(A[1:]))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))






"""
for i in range(M):
    b, c = map(int, input().split())
    A += [c] * b
    
A.sort(reverse=True)
print(sum(A[:N]))
"""

import bisect
A.sort()
A = [-1] + A
for i in range(M):
    b, c = map(int, input().split())
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    A = A[:idx] + [c] * b + A[idx:]
    A = A[-N:]
    #print(A)
print(sum(A))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Runtime Error):
import bisect
A.sort()
A = [-1] + A
for i in range(M):
    b, c = map(int, input().split())
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    A = A[:idx] + [c] * b + A[idx:]
    A = A[-N:]
    #print(A)
print(sum(A))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))


import bisect
A.sort()

A = [-1] + A

for i in range(M):
    b, c = map(int, input().split())
    idx = bisect.bisect_left(A, c)
    #print(A[:idx], A[idx:])
    A = A[:idx] + [c] * b + A[idx:]
    A = A[-N:]
    #print(A)
    
A.sort(reverse=True)
print(sum(A[:N]))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))

for i in range(M):
    b, c = map(int, input().split())
    A += [c] * b
    if len(A) > 10**7:
        A.sort(reverse=True)
        A = A[:N]
    
A.sort(reverse=True)
print(sum(A[:N]))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(M):
    b, c = map(int, input().split())
    A += [c] * b
    
A.sort(reverse=True)
print(sum(A[:N]))

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

CB = []
for i in range(M):
    B, C = map(int, input().split())
    CB.append([C, B])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(N):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= M:
        break
    #print(A)
        
print(sum(A))