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

for j in range(m):
    b, c = map(int, input().split())
    test.extend([c for i in range(b)])
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for j in range(m):
    b, c = map(int, input().split())
    test.extend([c for i in range(b)])
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    test.extend([c for i in range(b)])
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    test = test + [c for i in range(b)]
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    test = test + [c for i in range(b)]
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    test = test + [c for i in range(b)]
test.sort(reverse=True)
print(sum(test[:n]))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    if min(test) < c:
        test_1 = [c if test[j]<c else test[j] for j in range(b)]
        test = test_1 + test[b:]
        test.sort()
print(sum(test))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)

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

for i in range(m):
    b, c = map(int, input().split())
    if min(test) < c:
        test = [c if j<b and test[j]<c else test[j] for j in range(len(test))]
        test.sort()
print(sum(test))

Fixed submission:
from collections import defaultdict

n,m = map(int,input().split())
test=list(map(int,input().split()))

test_dict = defaultdict(int)

for j in test:
    test_dict[j] += 1

for k in range(m):
    b, c = map(int, input().split())
    test_dict[c] += b

count = 0
sum_val = 0
for l in sorted(test_dict, reverse=True):
    if n-count>0:
       sum_val += l * min(test_dict[l], n-count)
       count += min(test_dict[l], n-count)
    else:
       break
print(sum_val)