Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = []
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
BC = sorted(BC, reverse=True, key=lambda x:x[1])

first_idx = 0
last_idx = 0
for num, val in BC:
    if A[last_idx] >= val:
        break
    
    for _ in range(num):
        if last_idx >= N:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            break

        if A[last_idx] < val:
            last_idx += 1
        else:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            first_idx = last_idx
            break

        
    else:
        A[first_idx:last_idx] = [val] * (last_idx - first_idx)
        first_idx = last_idx

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = []
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
BC = sorted(BC, reverse=True, key=lambda x:x[1])

first_idx = 0
last_idx = 0
for num, val in BC:
    if last_idx >= N:
        break

    if A[last_idx] >= val:
        break
    

    for _ in range(num):
        if A[last_idx] < val:
            last_idx += 1
        else:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            first_idx = last_idx
            break

        if last_idx >= N:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            break
    else:
        A[first_idx:last_idx] = [val] * (last_idx - first_idx)
        first_idx = last_idx

print(sum(A))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = []
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
BC = sorted(BC, reverse=True, key=lambda x:x[1])

first_idx = 0
last_idx = 0
for num, val in BC:
    if A[last_idx] >= val:
        break
    if last_idx >= N:
        break

    for _ in range(num):
        if A[last_idx] < val:
            last_idx += 1
        else:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            first_idx = last_idx
            break

        if last_idx >= N:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            break
    else:
        A[first_idx:last_idx] = [val] * (last_idx - first_idx)
        first_idx = last_idx

print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = sorted(list(map(int, input().split())))
BC = []
for _ in range(M):
    BC.append(tuple(map(int, input().split())))
BC = sorted(BC, reverse=True, key=lambda x:x[1])

first_idx = 0
last_idx = 0
for num, val in BC:
    if last_idx >= N:
        break

    if A[last_idx] >= val:
        break
    

    for _ in range(num):
        if A[last_idx] < val:
            last_idx += 1
        else:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            first_idx = last_idx
            break

        if last_idx >= N:
            A[first_idx:last_idx] = [val] * (last_idx - first_idx)
            break
    else:
        A[first_idx:last_idx] = [val] * (last_idx - first_idx)
        first_idx = last_idx

print(sum(A))