Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if a_n[index] >= b_and_c[i][1] or index >= n:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))

Fixed submission:
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if index >= n:
        break
    elif a_n[index] >= b_and_c[i][1]:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if a_n[index] >= b_and_c[i][1] or index >= n:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, j + b_and_c[i][0]):
                if k == n:
                    break
                else:
                    if a_n[k] < b_and_c[i][1]:
                        a_n[k] = b_and_c[i][1]
                    else:
                        break
            index = k + 1
            break

print(sum(a_n))

Fixed submission:
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if index >= n:
        break
    elif a_n[index] >= b_and_c[i][1]:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()
a_n.reverse()

b_and_c.sort(lambda x : x[1])
changed = 0
for i in range(m):
    if a_n[n-1] >= b_and_c[i][1]:
        break
    for j in range(changed, b_and_c[i][0]):
        if j == n:
            break
        else:
            if a_n[j] < b_and_c[i][1]:
                a_n[j] = b_and_c[i][1]
                changed = j
            else:
                break

print(sum(a_n))

Fixed submission:
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if index >= n:
        break
    elif a_n[index] >= b_and_c[i][1]:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

for i in range(m):
    a_n.sort()
    for j in range(b_and_c[i][0]):
        if a_n[j] < b_and_c[i][1]:
            a_n[j] = b_and_c[i][1]
        else:
            break

print(sum(a_n))

Fixed submission:
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if index >= n:
        break
    elif a_n[index] >= b_and_c[i][1]:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
a_n = list(map(int,input(.split())))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

for i in range(b_and_c):
    sort(a_n)
    for j in range(b_and_c[i][0]):
        if a_n[j] < b_and_c[i][1]:
            a_n[j] = b_and_c[i][1]
        else:
            break

print(sum(a_n)

Fixed submission:
n,m = map(int,input().split())
a_n = list(map(int,input().split()))
b_and_c = []
for i in range(m):
    b_and_c.append(list(map(int,input().split())))

a_n.sort()

b_and_c.sort(key = lambda x : x[1], reverse = True)
index = 0
for i in range(m):
    if index >= n:
        break
    elif a_n[index] >= b_and_c[i][1]:
        break
    for j in range(index, n):
        if a_n[j] < b_and_c[i][1]:
            for k in range(j, min(j + b_and_c[i][0], n)):
                if a_n[k] < b_and_c[i][1]:
                    a_n[k] = b_and_c[i][1]
                else:
                    break
            index = k + 1
            break

print(sum(a_n))