Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a += [c] * b
a.sort()
ans = sum(a[-n:])
print(ans)

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        a.append(c)
a.sort()
ans = sum(a[-n:])
print(ans)

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.extend([c] * b)
a.sort()
ans = sum(a[-n:])
print(ans)

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.extend([c] * b)
a.sort()
ans = sum(a[-n:])
print(ans)

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
for _ in range(m):
    b, c = map(int, input().split())
    a.extend([c] * b)
a = list(reversed(sorted(a)))
ans = sum(a[:n])
print(ans)

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
for _ in range(m):
    b, c = map(int, input().split())
    k = bisect.bisect_right(a, c)
    a[:min(k, b)] = [c] * min(k, b)
    a.sort()
print(sum(a))

Fixed submission:
import collections
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
k = collections.Counter(a)
for _ in range(m):
    b, c = map(int, input().split())
    k[c] += b
ans = 0
count = 0
for key in list(reversed(sorted(list(k.keys())))):
    ans += (min(k[key], n - count)) * key
    count += min(k[key], n - count)
    if count == n:
        break
print(ans)