Buggy python submission (verdict: Time Limit Exceeded):
#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

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

a_list.sort(reverse = True)

print(sum(a_list[:N]))


Fixed submission:
#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

written_nums = []
for i in range(M):
    b, c = map(int, input().split())
    written_nums.append((c, b))

written_nums.sort(reverse = True)
append_list = []
for c, b in written_nums:
    append_list += [c for _ in range(b)]

    if len(append_list) >= N:
        break

a_list += append_list
a_list.sort(reverse = True)

print(sum(a_list[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))
written_nums = []

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

a_list = sorted(a_list)
written_nums = sorted(written_nums, reverse = True)

max_total = sum(a_list)
total = sum(a_list)
written_len = len(written_nums)
for i in range(min(N, written_len)):
    total -= a_list[i]
    total += written_nums[i]
    max_total = max(max_total, total)

print(max_total)


Fixed submission:
#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

written_nums = []
for i in range(M):
    b, c = map(int, input().split())
    written_nums.append((c, b))

written_nums.sort(reverse = True)
append_list = []
for c, b in written_nums:
    append_list += [c for _ in range(b)]

    if len(append_list) >= N:
        break

a_list += append_list
a_list.sort(reverse = True)

print(sum(a_list[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))
written_pairs = [None] * M

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

a_list = sorted(a_list, reverse = True)

print(sum(a_list[:N]))


Fixed submission:
#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

written_nums = []
for i in range(M):
    b, c = map(int, input().split())
    written_nums.append((c, b))

written_nums.sort(reverse = True)
append_list = []
for c, b in written_nums:
    append_list += [c for _ in range(b)]

    if len(append_list) >= N:
        break

a_list += append_list
a_list.sort(reverse = True)

print(sum(a_list[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
a_list = list(map(int, input().split()))
written_pairs = [None] * M

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

written_nums = sorted(written_nums, reverse = True)
a_list = sorted(a_list)

total = 0
len_nums = len(written_nums)
for i in range(N):
    if i < len_nums and a_list[i] < written_nums[i]:
        total += written_nums[i]
    else:
        total += a_list[i]
print(total)


Fixed submission:
#TLE
N, M = map(int, input().split())
a_list = list(map(int, input().split()))

written_nums = []
for i in range(M):
    b, c = map(int, input().split())
    written_nums.append((c, b))

written_nums.sort(reverse = True)
append_list = []
for c, b in written_nums:
    append_list += [c for _ in range(b)]

    if len(append_list) >= N:
        break

a_list += append_list
a_list.sort(reverse = True)

print(sum(a_list[:N]))