Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

BC = [[int(i) for i in input().split()] for _ in range(M)]

for B, C in BC :
    if C > min_A :
        A += [C] * B
        A = sorted(A)[B:]
        min_A = A[0]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

BC = [[int(i) for i in input().split()] for _ in range(M)]

for B, C in BC :
    if C > min_A :
        A += [C] * B
        A = sorted(A)[B:]
        min_A = A[0]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

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

N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

i = 0

for _ in range(M) :
    B, C = map(int, input().split())
    if C > min_A :
        for _ in range(B) :
            bisect.insort_left(A, C)
        i += B
        min_A = A[i]

print(sum(A[i:]))


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

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

N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

for _ in range(M) :
    B, C = map(int, input().split())
    if C > min_A :
        for _ in range(B) :
            bisect.insort_left(A, C)
        A = A[B:]
        min_A = A[0]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

BC = [[int(i) for i in input().split()] for _ in range(M)]
BC = sorted(BC, key=lambda x: x[1])[::-1]

i = 0
for B, C in BC :
    if i + B <= N :
        A += [C] * B
    else :
        A += [C] * (N - i)
        break

print(sum(sorted(A)[-1:-N-1:-1]))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

for _ in range(M) :
    B, C = map(int, input().split())
    if C > min_A :
        A += [C] * B
        A = sorted(A)[B:]
        min_A = A[0]

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = sorted([int(i) for i in input().split()])

min_A = A[0]

for _ in range(M) :
    B, C = map(int, input().split())
    if C >= min_A :
        A += [C] * B
        A = sorted(A)[B:]
        min_A = A[0]

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
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

print(sum(sorted(A)[-1:-N-1:-1]))

Fixed submission:
N, M = map(int, input().split())
A = [[1, int(i)] for i in input().split()]
BC = A + [[int(i) for i in input().split()] for _ in range(M)]

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

i = 0
res = 0

for B, C in BC :
    if i + B <= N :
        res += C * B
        i += B
    else :
        res += C * (N - i)
        break

print(res)