Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [-1] * m
c = [-1] * m
a.sort()
for j in range(m):
    b[j], c[j] = map(int, input().split())
    index = n
    for i in range(n):
        if c[j] < a[i]:
            index = i
            break
    for i in range(b[j]):
        a.insert(index, c[j])
    for i in range(b[j]):
        del a[0]

print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = [0] * m
for i in range(n):
    if index >= m or a[i] >= bc[index][1]:
        break
    a[i] = bc[index][1]
    count[index] += 1
    if count[index] >= bc[index][0]:
        index += 1

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [-1] * m
c = [-1] * m
a.sort()
for j in range(m):
    b[j], c[j] = map(int, input().split())
    count = 0
    for i in range(n):
        if count >= b[j]:
            break
        if a[i] > c[j]:
            break
        if a[i] < c[j]:
            count += 1
    index = n
    for i in range(n):
        if a[i] >= c[j]:
            index = i
            break
    for i in range(count):
        a.insert(index, c[j])
    for i in range(count):
        del a[0]

print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = [0] * m
for i in range(n):
    if index >= m or a[i] >= bc[index][1]:
        break
    a[i] = bc[index][1]
    count[index] += 1
    if count[index] >= bc[index][0]:
        index += 1

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = [-1] * m
c = [-1] * m
a.sort()
for j in range(m):
    b[j], c[j] = map(int, input().split())
    count = 0
    for i in range(n):
        if count >= b[j]:
            break
        if a[i] > c[j]:
            break
        if a[i] < c[j]:
            a[i] = c[j]
            count += 1
    a.sort()

print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = [0] * m
for i in range(n):
    if index >= m or a[i] >= bc[index][1]:
        break
    a[i] = bc[index][1]
    count[index] += 1
    if count[index] >= bc[index][0]:
        index += 1

print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
b = [-1] * m
c = [-1] * m
for j in range(m):
    a.sort()
    b[j], c[j] = map(int, input().split())
    count = 0
    for i in range(len(a)):
        if count >= b[j]:
            break
        if a[i] > c[j]:
            break
        if a[i] < c[j]:
            a[i] = c[j]
            count += 1

print(sum(a))

Fixed submission:
# -*- coding: utf-8 -*-
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = [tuple(map(int, input().split())) for _ in range(m)]
bc.sort(key=lambda t: t[1], reverse=True)

index = 0
count = [0] * m
for i in range(n):
    if index >= m or a[i] >= bc[index][1]:
        break
    a[i] = bc[index][1]
    count[index] += 1
    if count[index] >= bc[index][0]:
        index += 1

print(sum(a))