Buggy python submission (verdict: Time Limit Exceeded):
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

cards = [int(s) for s in input().split(' ')]
cards.sort()

bc_ary = []
for i in range(M):
    bc_ary.append([int(s) for s in input().split(' ')])

for bc in bc_ary:
    cnt = 0
    idx = 0
    while idx < N and cnt < bc[0]:
        mincard = min(cards)
        if mincard >= bc[1]:
            break
        if mincard < bc[1]:
            cards[cards.index(mincard)] = bc[1]
            cnt += 1
        idx += 1

print(sum(cards))


Fixed submission:
import os, sys, re, math, heapq

N, M = [int(s) for s in input().split(' ')]

dic = {}
cards =  [int(s) for s in input().split(' ')]
for c in cards:
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1

for i in range(M):
    b,c = [int(s) for s in input().split(' ')]
    if c in dic:
        dic[c] += b
    else:
        dic[c] = b

keys = sorted(list(dic.keys()), reverse=True)

ret = 0
for k in keys:
    ret += min(N, dic[k]) * k
    N = N - dic[k]
    if N <= 0:
        break

print(ret)

Buggy python submission (verdict: Time Limit Exceeded):
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

cards = [int(s) for s in input().split(' ')]
cards.sort()

bc_ary = []
for i in range(M):
    bc_ary.append([int(s) for s in input().split(' ')])

for bc in bc_ary:
    cnt = 0
    idx = 0
    while idx < N and cnt < bc[0]:
        mincard = min(cards)
        if mincard < bc[1]:
            cards[cards.index(mincard)] = bc[1]
            cnt += 1
        idx += 1

print(sum(cards))


Fixed submission:
import os, sys, re, math, heapq

N, M = [int(s) for s in input().split(' ')]

dic = {}
cards =  [int(s) for s in input().split(' ')]
for c in cards:
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1

for i in range(M):
    b,c = [int(s) for s in input().split(' ')]
    if c in dic:
        dic[c] += b
    else:
        dic[c] = b

keys = sorted(list(dic.keys()), reverse=True)

ret = 0
for k in keys:
    ret += min(N, dic[k]) * k
    N = N - dic[k]
    if N <= 0:
        break

print(ret)

Buggy python submission (verdict: Time Limit Exceeded):
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

cards = [int(s) for s in input().split(' ')]
cards.sort()

bc_ary = []
for i in range(M):
    bc_ary.append([int(s) for s in input().split(' ')])

for bc in bc_ary:
    cnt = 0
    idx = 0
    while idx < N and cnt < bc[0]:
        if cards[idx] < bc[1]:
            cards[idx] = bc[1]
            cnt += 1
        idx += 1

    cards.sort()

print(sum(cards))


Fixed submission:
import os, sys, re, math, heapq

N, M = [int(s) for s in input().split(' ')]

dic = {}
cards =  [int(s) for s in input().split(' ')]
for c in cards:
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1

for i in range(M):
    b,c = [int(s) for s in input().split(' ')]
    if c in dic:
        dic[c] += b
    else:
        dic[c] = b

keys = sorted(list(dic.keys()), reverse=True)

ret = 0
for k in keys:
    ret += min(N, dic[k]) * k
    N = N - dic[k]
    if N <= 0:
        break

print(ret)

Buggy python submission (verdict: Time Limit Exceeded):
import os, sys, re, math

N, M = [int(s) for s in input().split(' ')]

cards = [int(s) for s in input().split(' ')]
cards.sort()

for i in range(M):
    b, c = [int(s) for s in input().split(' ')]
    idx = 0
    while idx < b and cards[idx] < c:
        cards[idx] = c
        idx += 1
    cards.sort()

print(sum(cards))


Fixed submission:
import os, sys, re, math, heapq

N, M = [int(s) for s in input().split(' ')]

dic = {}
cards =  [int(s) for s in input().split(' ')]
for c in cards:
    if c in dic:
        dic[c] += 1
    else:
        dic[c] = 1

for i in range(M):
    b,c = [int(s) for s in input().split(' ')]
    if c in dic:
        dic[c] += b
    else:
        dic[c] = b

keys = sorted(list(dic.keys()), reverse=True)

ret = 0
for k in keys:
    ret += min(N, dic[k]) * k
    N = N - dic[k]
    if N <= 0:
        break

print(ret)