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

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

BC = []

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])

BC.reverse()

idx = N-1

for i in range(M):
    B, C = BC[i]

    while idx >= 0:
        if A[idx] < C:
            idx += 1
            break
        idx -= 1
    else:
        break

    if B >= idx:
        A = [C]*idx + A[idx:]
        break
    else:
        A = A[B:idx] + [C]*B + A[idx:]

print(sum(A))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

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

BC = []

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])

BC.reverse()

for i in range(M):
    B, C = BC[i]
    in_idx = bisect.bisect_right(A,C)

    if B >= in_idx:
        A = [C]*in_idx + A[in_idx:]
        break
    else:
        A = A[B:in_idx] + [C]*B + A[in_idx:]

print(sum(A))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

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

BC = []

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])

BC.reverse()

for i in range(M):
    B, C = BC[i]
    in_idx = bisect.bisect_right(A,C)

    if B >= in_idx:
        A = [C]*in_idx + A[in_idx:]
        break
    else:
        A = A[B:in_idx] + [C]*B + A[in_idx:]

print(sum(A))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

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

A.sort()

for i in range(M):
    B, C = list(map(int,input().split()))
    in_idx = bisect.bisect_right(A,C)

    if B >= in_idx:
        A = [C]*in_idx + A[in_idx:]
    else:
        A = A[B:in_idx] + [C]*B + A[in_idx:]

print(sum(A))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

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

A.sort()

for i in range(M):
    B, C = list(map(int,input().split()))
    in_idx = min(B,bisect.bisect_right(A,C))
    A = [C]*in_idx + A[in_idx:]
    A.sort()

print(sum(A))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

for i in range(M):
    B, C = list(map(int,input().split()))
    A += [C]*B

A.sort()

print(sum(A[len(A)-N:]))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

for i in range(M):
    B, C = list(map(int,input().split()))
    A += [C]*B

A.sort()
A.reverse()

print(sum(A[:N]))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)

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

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

for i in range(M):
    B, C = list(map(int,input().split()))
    A += [C]*B

A.sort()
A.reverse()

print(sum(A[:N]))

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

A = [-1]
A += list(map(int,input().split()))
A.sort()

BC = [[1,-1]]

for i in range(M):
    BC.append(list(map(int,input().split())))

BC = sorted(BC, key=lambda x: x[1])


x = 0

for i in range(N):
    if A[-1] > BC[-1][1]:
        x += A.pop()
        
    else:
        x += BC[-1][1]
        BC[-1][0] -= 1
        if BC[-1][0] == 0:
            BC.pop()

print(x)