Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if idx + BC[i][0] >= N :
        BC[i][0] = N-idx
    if A[idx + BC[i][0]] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx + j])
    idx += BC[i][0]
    if idx > N-1:break
print(ans)

            

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

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if idx + BC[i][0] >= N :
        BC[i][0] = N-idx
    if A[idx + BC[i][0]-1] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx + j])
        break
    idx += BC[i][0]
    if idx > N-1:break
print(ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if idx + BC[i][0] >= N :
        BC[i][0] = N-idx
    if A[idx + BC[i][0]] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx + j])
        print(A[idx:idx+j],j)
    idx += BC[i][0]
    if idx > N-1:break
print(ans)

            

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

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if idx + BC[i][0] >= N :
        BC[i][0] = N-idx
    if A[idx + BC[i][0]-1] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx + j])
        break
    idx += BC[i][0]
    if idx > N-1:break
print(ans)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if A[idx+BC[i][0]-1] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx+j])
        break
    idx += BC[i][0]
    
print(ans)

            

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

A.sort()
BC.sort(reverse = True, key = lambda x:x[1])
ans = sum(A)
idx = 0

for i in range(M):
    if idx + BC[i][0] >= N :
        BC[i][0] = N-idx
    if A[idx + BC[i][0]-1] < BC[i][1]:
        ans += BC[i][1] * BC[i][0] - sum(A[idx:idx+BC[i][0]])
    else:
        for j in range(BC[i][0]):
            if A[idx+j] >= BC[i][1]:
                break
        ans += BC[i][1] * j - sum(A[idx:idx + j])
        break
    idx += BC[i][0]
    if idx > N-1:break
print(ans)