Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cards = list(map(int, input().split()))

cs = []
for i in range(m):
    b, c = map(int, input().split())
    cs += [c] * b

cards += cs
cards.sort(reverse = True)
    
print(sum(cards[:n]))

Fixed submission:
n, m = map(int, input().split())
cards = [[1, int(i)] for i in input().split()]
bc = [list(map(int, input().split())) for _ in range(m)]
cards += bc
cards.sort(key = lambda x: x[1], reverse = True)

l = []
flag = 0
for i in cards:
    for j in range(i[0]):
        l.append(i[1])
        if len(l) == n:
            flag = 1
            break
    if flag:
        break

print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cards = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

c = []
flag = 0
for i in bc:
    for j in range(i[0]):
        c.append(i[1])
        
cards += c
cards.sort(reverse = True)
    
print(sum(cards[:n]))

Fixed submission:
n, m = map(int, input().split())
cards = [[1, int(i)] for i in input().split()]
bc = [list(map(int, input().split())) for _ in range(m)]
cards += bc
cards.sort(key = lambda x: x[1], reverse = True)

l = []
flag = 0
for i in cards:
    for j in range(i[0]):
        l.append(i[1])
        if len(l) == n:
            flag = 1
            break
    if flag:
        break

print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cards = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

c = []
for i in bc:
    for j in range(i[0]):
        c.append(i[1])
        
c.sort(reverse = True)

for i in c:
    if min(cards) < i:
        cards[cards.index(min(cards))] = i
    else:
        break
    
print(sum(cards))

Fixed submission:
n, m = map(int, input().split())
cards = [[1, int(i)] for i in input().split()]
bc = [list(map(int, input().split())) for _ in range(m)]
cards += bc
cards.sort(key = lambda x: x[1], reverse = True)

l = []
flag = 0
for i in cards:
    for j in range(i[0]):
        l.append(i[1])
        if len(l) == n:
            flag = 1
            break
    if flag:
        break

print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cards = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]

c = []
for i in bc:
    for j in range(i[0]):
        c.append(i[1])
        
c.sort(reverse = True)

for i in c:
    cards.sort()
    if cards[0] < i:
        cards[0] = i
    else:
        break
    
print(sum(cards))

Fixed submission:
n, m = map(int, input().split())
cards = [[1, int(i)] for i in input().split()]
bc = [list(map(int, input().split())) for _ in range(m)]
cards += bc
cards.sort(key = lambda x: x[1], reverse = True)

l = []
flag = 0
for i in cards:
    for j in range(i[0]):
        l.append(i[1])
        if len(l) == n:
            flag = 1
            break
    if flag:
        break

print(sum(l))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cards = list(map(int, input().split()))
bc = [list(map(int, input().split()))[::-1] for _ in range(m)]
bc.sort(reverse = True)

for i in bc:
    for j in range(i[1]):
        cards.sort()
        i.sort(reverse = True)

        if cards[0] < i[0]:
            cards[0] = i[0]
            i[1] -= 1
        
        if i[1] == 0:
            i[0] = 0

print(sum(cards))

Fixed submission:
n, m = map(int, input().split())
cards = [[1, int(i)] for i in input().split()]
bc = [list(map(int, input().split())) for _ in range(m)]
cards += bc
cards.sort(key = lambda x: x[1], reverse = True)

l = []
flag = 0
for i in cards:
    for j in range(i[0]):
        l.append(i[1])
        if len(l) == n:
            flag = 1
            break
    if flag:
        break

print(sum(l))