Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = sorted([int(i) for i in input().split()], reverse=True)

cs = []

for _ in range(m):
    b, c = map(int, input().split())
    cs += [c] * b

cs = sorted(cs, reverse=True)

ind_a, ind_c = 0, 0
ans = 0

for i in range(n):
    if len(cs)-1 >= ind_c:
        if a[ind_a] >= cs[ind_c]:
            ans += a[ind_a]
            ind_a += 1
        else:
            ans += cs[ind_c]
            ind_c += 1
    else:
        ans += a[ind_a]
        ind_a += 1

print(ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

n, m = map(int, input().split())
a = [-(int(i)) for i in input().split()]

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc += [-c] * b

heapq.heapify(a)
heapq.heapify(bc)
ans = 0
x, y = [], []

for _ in range(n):
    if x == []:
        x.append(heapq.heappop(a))
    if y == [] and len(bc) > 0:
        y.append(heapq.heappop(bc))

    if x <= y:
        ans += x.pop()
    else:
        if y != []:
            ans += y.pop()
        else:
            ans += x.pop()

print(-ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

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

heapq.heapify(a) # in-place

for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        temp = heapq.heappop(a)
        if c > temp:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, temp)
            break

print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = list(map(int, input().split()))

heapq.heapify(a) # in-place

for _ in range(m):
    b, c = map(int, input().split())
    for _ in range(b):
        temp = heapq.heappop(a)
        if c > temp:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, temp)
            break

print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

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)

heapq.heapify(a)

ans = 0

for _ in range(n):
    ans += heapq.heappop(a)

print(-ans)

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

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

for _ in range(m):
    b, c = map(int, input().split())
    a += ([c] * b)

print(sum(sorted(a,reverse=True)[:n]))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
def main():

    import heapq

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

    heapq.heapify(a) # in-place

    for _ in range(m):
        temp = []
        b, c = map(int, input().split())
        for i in range(b):
            temp.append(heapq.heappop(a))
            if c > temp[-1]:
                temp[-1] = c
            else:
                break
        
        for j in temp:
            heapq.heappush(a, j)

    print(sum(a))

if __name__ == '__main__':
    main()

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

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

heapq.heapify(a) # in-place

for _ in range(m):
    b, c = map(int, input().split())
    for i in range(b):
        temp = heapq.heappop(a) 
        if c > temp:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, temp)
            break


print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import heapq

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

heapq.heapify(a) # in-place

for _ in range(m):
    temp = []
    b, c = map(int, input().split())
    for i in range(b):
        temp.append(heapq.heappop(a))
        if c > temp[-1]:
            temp[-1] = c
        else:
            break
    for j in temp:
        heapq.heappush(a, j)

print(sum(a))

Fixed submission:
n, m = map(int, input().split())
a = list(map(int, input().split()))

bc = []

for _ in range(m):
    b, c = map(int, input().split())
    bc.append((b, c))

bc = sorted(bc, key=lambda x: x[1], reverse=True)

cnt = 0

for b, c in bc:
    cnt += b
    a += [c] * b
    if cnt >= n:
        break

print(sum(sorted(a,reverse=True)[:n]))