Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort()

i = 0
for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        temp = a[i]
        if temp < number:
            a[i] = number
        else:
            print(sum(a))
            exit()
        times -= 1
        i += 1
print(sum(a))

Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort()


i = 0
for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        try:
            temp = a[i]
        except IndexError:
            print(sum(a))
            exit()
        
        if temp < number:
            a[i] = number
        else:
            print(sum(a))
            exit()
        times -= 1
        i += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]
x.sort(key=sortsecond)

for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        temp = min(a)
        if temp < number:
            a[a.index(temp)] = number
        else:
            break
        times -= 1


print(sum(a))

Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort()


i = 0
for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        try:
            temp = a[i]
        except IndexError:
            print(sum(a))
            exit()
        
        if temp < number:
            a[i] = number
        else:
            print(sum(a))
            exit()
        times -= 1
        i += 1
print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort(reverse=True)

for j in range(m):
    
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :
        if min(a) < number:
            a[a.index(min(a))] = number
        else:
            break
        times -= 1


print(sum(a))

Fixed submission:
import sys
input = sys.stdin.readline

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [0]*m
c = [0]*m
sortsecond = lambda val: val[1]

x = [list(map(int, input().split())) for _ in range(m)]

x.sort(key=sortsecond)

a.sort()


i = 0
for j in range(m):
    times = x[-(j+1)][0]
    number = x[-(j+1)][1]
    while times > 0 :

        try:
            temp = a[i]
        except IndexError:
            print(sum(a))
            exit()
        
        if temp < number:
            a[i] = number
        else:
            print(sum(a))
            exit()
        times -= 1
        i += 1
print(sum(a))