Buggy python submission (verdict: Time Limit Exceeded):
import collections

def int_cards(cards, operations):
    col_cards = collections.deque(sorted(cards))
    operations.sort(reverse=True)
    for c, b in operations:
        for _ in range(b):
            if col_cards[0] > c:
                break
            col_cards.popleft()
            col_cards.append(c)
            
    print(sum(col_cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(A, operations)

Fixed submission:
import collections

def int_cards(N, cards, operations):
    col_cards = collections.deque(sorted(cards), N)
    operations.sort(reverse=True)
    for c, b in operations:
        for _ in range(b):
            if col_cards[0] >= c:
                break
            col_cards.popleft()
            col_cards.append(c)
            
    print(sum(col_cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(N, A, operations)

Buggy python submission (verdict: Time Limit Exceeded):
def int_cards(cards, operations):
    cards = sorted(cards)
    operations.sort(reverse=True)
    for i in range(len(operations)):
        for j in range(operations[i][1]):
            if cards[0] > operations[i][0]:
                break
            cards.pop(0)
            cards.append(operations[i][0])
            
    print(sum(cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(A, operations)

Fixed submission:
import collections

def int_cards(N, cards, operations):
    col_cards = collections.deque(sorted(cards), N)
    operations.sort(reverse=True)
    for c, b in operations:
        for _ in range(b):
            if col_cards[0] >= c:
                break
            col_cards.popleft()
            col_cards.append(c)
            
    print(sum(col_cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(N, A, operations)

Buggy python submission (verdict: Time Limit Exceeded):
def int_cards(cards, operations):
    cards = sorted(cards)
    operations.sort(reverse=True)
    changed = False
    for i in range(len(operations)):
        if changed:
            cards = sorted(cards)
            changed = False
        for j in range(operations[i][1]):
            if cards[j] < operations[i][0]:
                cards[j] = operations[i][0]
                changed = True
            else:
                break
            
    print(sum(cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(A, operations)

Fixed submission:
import collections

def int_cards(N, cards, operations):
    col_cards = collections.deque(sorted(cards), N)
    operations.sort(reverse=True)
    for c, b in operations:
        for _ in range(b):
            if col_cards[0] >= c:
                break
            col_cards.popleft()
            col_cards.append(c)
            
    print(sum(col_cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(N, A, operations)

Buggy python submission (verdict: Time Limit Exceeded):
def int_cards(cards, operations):
    operations.sort(reverse=True)
    for i in range(len(operations)):
        cards = sorted(cards)
        for j in range(operations[i][1]):
            if cards[j] < operations[i][0]:
                cards[j] = operations[i][0]
            
    print(sum(cards))

N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])

int_cards(A, operations)   

Fixed submission:
import collections

def int_cards(N, cards, operations):
    col_cards = collections.deque(sorted(cards), N)
    operations.sort(reverse=True)
    for c, b in operations:
        for _ in range(b):
            if col_cards[0] >= c:
                break
            col_cards.popleft()
            col_cards.append(c)
            
    print(sum(col_cards))
    
N, M = map(int, input().split())
A = list(map(int,input().split()))
operations = []
for i in range(M):
    B, C = map(int, input().split())
    operations.append([C, B])
    
int_cards(N, A, operations)