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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    if idx <= N:
        X[idx:S[i][0]] = [S[i][1]] * S[i][0]
        idx += S[i][0]
    else:
        break

for i in range(N):
    A[i] = max(A[i], X[i])

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    if idx < len(A):
        X[idx:S[i][0]] = [S[i][1]] * S[i][0]
        idx += S[i][0]

for i in range(idx):
    A[i] = max(A[i], X[i])

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    if idx < len(A):
        X[idx:S[i][0]] = [S[i][1]] * S[i][0]
        idx += S[i][0]

for i in range(len(A)):
    A[i] = max(A[i], X[i])

print(sum(A))


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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    for _ in range(S[i][0]):
        if idx < len(A):
            X[idx]=S[i][1]
            idx += 1

for i in range(idx):
    A[i] = max(A[i], X[i])

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    for _ in range(S[i][0]):
        if idx < len(A):
            X[idx]=S[i][1]
            idx += 1

for i in range(idx):
    A[i] = max(A[i], X[i])

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

X = [0]*(len(A))
idx = 0

for i in range(M):
    for _ in range(S[i][0]):
        if idx <= len(A):
            X[idx]=S[i][1]
            idx += 1

for i in range(idx):
    A[i] = max(A[i], X[i])

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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.sort()

    for i in range(B):
        if A[i]<C:
            A[i] = C

print(sum(A))

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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))

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

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

for _ in range(M):
    B,C = map(int, input().split())
    A.sort()

    for i in range(B):
        if A[i]<C:
            A[i] = C
            print(A)
            print('-----------\n')

print(sum(A))


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

A = list(map(int, input().split()))
S = [list(map(int,input().split())) for i in range(M)]

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

idx = 0

for i in range(M):
    for j in range(idx, min(N, idx + S[i][0])):
        if A[j] < S[i][1]:
            A[j] = S[i][1]
            idx += 1
        else:
            break

print(sum(A))