Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import re
import bisect
from datetime import datetime as dt

n, m = map(int, input().split())
A = list(map(int, input().split()))
for i in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        A.append(c)
A.sort()
print(sum(A[len(A)-n:len(A)]))


Fixed submission:
n,m=map(int,input().split())
a = list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in [0]*m]
a.sort()
bc.sort(key=lambda x: x[1], reverse=True)  
i=0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j] < c:
            a[j] = c
            i+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import re
import bisect
from datetime import datetime as dt


def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right


n, m = map(int, input().split())
A = list(map(int, input().split()))
for i in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        A.append(c)
A = quick_sort(A)
print(sum(A[len(A)-n:len(A)]))


Fixed submission:
n,m=map(int,input().split())
a = list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in [0]*m]
a.sort()
bc.sort(key=lambda x: x[1], reverse=True)  
i=0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j] < c:
            a[j] = c
            i+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
n, m = map(int, input().split())
A = list(map(int, input().split()))
for i in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        A.append(c)
A.sort()
print(sum(A[len(A)-n:len(A)]))


Fixed submission:
n,m=map(int,input().split())
a = list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in [0]*m]
a.sort()
bc.sort(key=lambda x: x[1], reverse=True)  
i=0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j] < c:
            a[j] = c
            i+=1
        else:
            break
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# coding: utf-8
import re
import bisect
from datetime import datetime as dt
n, m = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
B = []
C = []
for i in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        if A[i] < c:
            A[i] = c
        else:
            break
    A.sort()
print(sum(A))


Fixed submission:
n,m=map(int,input().split())
a = list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in [0]*m]
a.sort()
bc.sort(key=lambda x: x[1], reverse=True)  
i=0
for b,c in bc:
    for j in range(i,min(n,i+b)):
        if a[j] < c:
            a[j] = c
            i+=1
        else:
            break
print(sum(a))