Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

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

change_list = []

for line in range(M):
    change_count, change_number = map(int, input().split())

    change_list.append([change_count, change_number])


change_list.sort(key=itemgetter(1), reverse=True)

index = 0
for change in change_list:
    for i in range(change[0]):
        if in_hand[i+index] < change[1]:
            in_hand[i+index] = change[1]
        else:
            break
    else:
        index += change[0]
        continue
    break

print(sum(in_hand))


Fixed submission:
from operator import itemgetter

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

change_list = []

for line in range(M):
    change_count, change_number = map(int, input().split())

    change_list.append([change_count, change_number])


change_list.sort(key=itemgetter(1), reverse=True)

index = 0
try:
    for change in change_list:
        for i in range(change[0]):
            if in_hand[i+index] < change[1]:
                in_hand[i+index] = change[1]
            else:
                break
        else:
            index += change[0]
            continue
        break
except:
    pass

print(sum(in_hand))

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

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

change_list = []

for line in range(M):
    change_count, change_number = map(int, input().split())

    change_list.append([change_count, change_number])


change_list.sort(key=itemgetter(1), reverse=True)


for change in change_list:
    in_hand.sort()
    for i in range(change[0]):
        if in_hand[i] < change[1]:
            in_hand[i] = change[1]
        else:
            break
    else:
        continue
    break

print(sum(in_hand))


Fixed submission:
from operator import itemgetter

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

change_list = []

for line in range(M):
    change_count, change_number = map(int, input().split())

    change_list.append([change_count, change_number])


change_list.sort(key=itemgetter(1), reverse=True)

index = 0
try:
    for change in change_list:
        for i in range(change[0]):
            if in_hand[i+index] < change[1]:
                in_hand[i+index] = change[1]
            else:
                break
        else:
            index += change[0]
            continue
        break
except:
    pass

print(sum(in_hand))

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

for line in range(M):
    change_count, change_number = map(int, input().split())
    in_hand.sort()
    for i in range(change_count):
        if in_hand[i] < change_number:
            in_hand[i] = change_number
        else:
            break

print(sum(in_hand))

Fixed submission:
from operator import itemgetter

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

change_list = []

for line in range(M):
    change_count, change_number = map(int, input().split())

    change_list.append([change_count, change_number])


change_list.sort(key=itemgetter(1), reverse=True)

index = 0
try:
    for change in change_list:
        for i in range(change[0]):
            if in_hand[i+index] < change[1]:
                in_hand[i+index] = change[1]
            else:
                break
        else:
            index += change[0]
            continue
        break
except:
    pass

print(sum(in_hand))