Buggy python submission (verdict: Runtime Error):
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            if c < len(A):
                A[c] = p[1]
                c += 1
            else:
                break
        else:
            break

print(sum(A))

Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            if c < N:
                A[c] = p[1]
                c += 1
            else:
                break
        else:
            break

print(sum(A))


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    if c < len(A):
        for b in range(p[0]):
            if p[1] > A[c]:
                A[c] = p[1]
                c += 1
            else:
                break

print(sum(A))


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x:x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
        else:
            break

print(sum(A))


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heapreplace

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
heapify(A)

for i in range(M):
    B, C = map(int, input("").split(" "))

    for j in range(B):
        if A[0] < C:
            heapreplace(A, C)
        else:
            break

print(sum(A))

Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heapreplace

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
heapify(A)

for i in range(M):
    B, C = map(int, input("").split(" "))

    c = 0
    for j in range(B):
        if A[0] < C:
            heapreplace(A, C)
            c += 1
        else:
            break

c = 0
for a in A:
    c += a

print(c)


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappop

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
heapify(A)

for i in range(M):
    B, C = map(int, input("").split(" "))

    c = 0
    for j in range(B):
        if A[0] < C:
            heappop(A)
            c += 1
        else:
            break

    A.extend([C]*c)
    heapify(A)

c = 0
for a in A:
    c += a

print(c)


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappushpop

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
heapify(A)

for i in range(M):
    B, C = map(int, input("").split(" "))

    for j in range(B):
        if A[0] < C:
            heappushpop(A, C)
        else:
            break

c = 0
for a in A:
    c += a

print(c)


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

for i in range(M):
    B, C = map(int, input("").split(" "))

    c = -1
    for j in range(B):
        if C > A[j]:
            c = j
        else:
            break

    if c >= 0:
        A = A[c+1:]

        b = bisect_left(A, C)
        A = A[:b] + [C]*(c+1) + A[b:]

c = 0
for a in A:
    c += a

print(c)


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

for i in range(M):
    B, C = map(int, input("").split(" "))

    c = -1
    for j in range(B):
        if C > A[j]:
            c = j
        else:
            break

    if c >= 0:
        A = A[c+1:]

    A.extend([C]*(c+1))
    A.sort()


c = 0
for a in A:
    c += a

print(c)


Fixed submission:
N, M = map(int, input("").split(" "))

A = map(int, input("").split(" "))
A = list(A)
A.sort()

potentials = []
for i in range(M):
    B, C = map(int, input("").split(" "))

    potentials.append((B, C))

potentials.sort(key=lambda x: x[1], reverse=True)

c = 0
for p in potentials:
    for b in range(p[0]):
        if p[1] > A[c]:
            A[c] = p[1]
            c += 1
            if c >= len(A):
                break
        else:
            break
    if c >= len(A):
        break

print(sum(A))