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

N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

for i in range(M):
    b, c = [int(_) for _ in input().split()]
    cnt = 0
    for j in range(b):
        if len(A) <= j: break
        if A[j] >= c: break
        cnt += 1
    if cnt > 0:
        # A = A[cnt:]
        idx = bisect.bisect_left(A, c)
        A = A[cnt:idx] + ([c] * cnt) + A[idx:]
        # print(A)

print(sum(A))

Fixed submission:
N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

BC = []
for i in range(M):
    v = [v for v in input().split()]
    BC.append((int(v[0]), int(v[1])))

BC.sort(key=lambda x: -x[1])

i = 0

fin = False
for b, c in BC:
    for _ in range(b):
        if i >= N:
            fin = True
            break

        if A[i] < c:
            A[i] = c
            i += 1
        else:
            fin = True
            break
    if fin:
        break
print(sum(A))

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

N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

for i in range(M):
    b, c = [int(_) for _ in input().split()]
    cnt = 0
    for j in range(b):
        if len(A) <= j: break
        if A[j] >= c: break
        cnt += 1
    if cnt > 0:
        # A = A[cnt:]
        idx = bisect.bisect_left(A, c)
        A = A[cnt:idx] + ([c] * cnt) + A[idx:]
        # print(A)

print(sum(A))

Fixed submission:
N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

BC = []
for i in range(M):
    v = [v for v in input().split()]
    BC.append((int(v[0]), int(v[1])))

BC.sort(key=lambda x: -x[1])

i = 0

fin = False
for b, c in BC:
    for _ in range(b):
        if i >= N:
            fin = True
            break

        if A[i] < c:
            A[i] = c
            i += 1
        else:
            fin = True
            break
    if fin:
        break
print(sum(A))

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

N, M = [int(_) for _ in input().split()]
A = []
for a in input().split():
    hq.heappush(A, int(a))

for i in range(M):
    b, c = [int(_) for _ in input().split()]

    for j in range(b):
        if not A: break
        if A[0] >= c: break
        if A[0] < c:
            hq.heappop(A)
            hq.heappush(A, c)

print(sum(A))

Fixed submission:
N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

BC = []
for i in range(M):
    v = [v for v in input().split()]
    BC.append((int(v[0]), int(v[1])))

BC.sort(key=lambda x: -x[1])

i = 0

fin = False
for b, c in BC:
    for _ in range(b):
        if i >= N:
            fin = True
            break

        if A[i] < c:
            A[i] = c
            i += 1
        else:
            fin = True
            break
    if fin:
        break
print(sum(A))

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

N, M = [int(_) for _ in input().split()]
A = []
for a in input().split():
    hq.heappush(A, int(a))

for i in range(M):
    b, c = [int(_) for _ in input().split()]

    for j in range(b):
        if not A: break
        if A[0] >= c: break
        if A[0] < c:
            hq.heappop(A)
            hq.heappush(A, c)

print(sum(A))

Fixed submission:
N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
A.sort()

BC = []
for i in range(M):
    v = [v for v in input().split()]
    BC.append((int(v[0]), int(v[1])))

BC.sort(key=lambda x: -x[1])

i = 0

fin = False
for b, c in BC:
    for _ in range(b):
        if i >= N:
            fin = True
            break

        if A[i] < c:
            A[i] = c
            i += 1
        else:
            fin = True
            break
    if fin:
        break
print(sum(A))