Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
ls = []
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    ls.append((b, c))
    ct[c] += b
ls.sort(key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in ls:
    if cnt + b < n:
        a_ls += [c] * b
    else:
        a_ls += [c] * (b - cnt)
a_ls.sort()
print(sum(a_ls[-n:]))

Fixed submission:
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
ls = []
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    ls.append((b, c))
    ct[c] += b
ls.sort(key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in ls:
    if cnt + b < n:
        a_ls += [c] * b
        cnt += b
    else:
        a_ls += [c] * (n - cnt)
        break
a_ls.sort()
print(sum(a_ls[-n:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    a_ls += [c] * b
    ct[c] += b
a_ls.sort()
print(sum(a_ls[-n:]))

Fixed submission:
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
ls = []
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    ls.append((b, c))
    ct[c] += b
ls.sort(key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in ls:
    if cnt + b < n:
        a_ls += [c] * b
        cnt += b
    else:
        a_ls += [c] * (n - cnt)
        break
a_ls.sort()
print(sum(a_ls[-n:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
import heapq
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
for i in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    ind = bisect.bisect_left(a_ls, c)
    a_ls = (a_ls[:ind] + [c] * b + a_ls[ind:])[-n:]
print(sum(sorted(a_ls)[-n:]))

Fixed submission:
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
ls = []
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    ls.append((b, c))
    ct[c] += b
ls.sort(key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in ls:
    if cnt + b < n:
        a_ls += [c] * b
        cnt += b
    else:
        a_ls += [c] * (n - cnt)
        break
a_ls.sort()
print(sum(a_ls[-n:]))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls.sort()
for i in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    ind = bisect.bisect_left(a_ls, c)
    a_ls = (a_ls[:ind] + [c] * b + a_ls[ind:])[-n:]
print(sum(sorted(a_ls)[-n:]))

Fixed submission:
import sys
from collections import Counter
n, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(a_ls)
ls = []
for j in range(m):
    b, c = [int(i) for i in sys.stdin.readline().split()]
    if ct.get(c) is None:
        ct[c] = 0
    ls.append((b, c))
    ct[c] += b
ls.sort(key=lambda x:x[1], reverse=True)
cnt = 0
for b, c in ls:
    if cnt + b < n:
        a_ls += [c] * b
        cnt += b
    else:
        a_ls += [c] * (n - cnt)
        break
a_ls.sort()
print(sum(a_ls[-n:]))