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

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = list(map(int, input().split()))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
ret = 0
for b, c in BC:
    r = bisect.bisect_left(A, c)
    # l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, r)
    ret += c * b
    A = A[b:]

print(sum(A)+ret)

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = list(map(int, input().split()))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
ret = 0
tmp = 0
for b, c in BC:
    l, r = serach(c, A[tmp:])
    if r <= 0:
        break
    b = min(b, r)
    ret += c * b
    tmp += b

print(sum(A[tmp:])+ret)

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = list(map(int, input().split()))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
ret = 0
for b, c in BC:
    l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, r)
    ret += c * b
    A = A[b:]

print(sum(A)+ret)


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = np.array(list(map(int, input().split())))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)
A.sort()
for b, c in BC:
    l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, r)
    A = np.hstack([A[b:], [c] * b])

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = np.array(list(map(int, input().split())))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)

for b, c in BC:
    A.sort()
    l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, r)
    A[:b] = c

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
def serach(target, array):
    l, r = -1, len(array)
    while r - l > 1:
        m = (l+r)//2
        if array[m] < target:
            l = m
        else:
            r = m
    return l, r

N, M = map(int, input().split())
A = np.array(list(map(int, input().split())))

BC = [list(map(int, input().split())) for _ in range(M)]
BC.sort(key=lambda x:x[1], reverse=True)

for b, c in BC:
    A.sort()
    l, r = serach(c, A)
    if r <= 0:
        break
    b = min(b, len(A[:r]))
    A[:b] = c

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
for i in range(M):
    b, c = map(int, input().split())
    A.append([b, c])

total = 0
D = []
A = sorted(A, key=lambda x:x[1], reverse=True)
for b, c in A:
    if len(D) >= N:
        break
    D.extend([c]*b)
print(sum(D[:N]))