Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
res = 0
for b, c in ls2:
    if left == n or c <= ls[left]:
        break
    i = bisect.bisect(ls[left:], c)
    i = min(i, b)
    res += c * i
    left += i
print(res + sum(ls[left:]))

Fixed submission:
n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
right = 0
index = 0
res = 0
while left < n:
    if index == len(ls2):
        break
    b, c = ls2[index]
    if c <= ls[left]:
        break
    right = left + 1
    while right <= n:
        if right == n:
            res += c * (right - left)
            break
        if ls[right] <= c and right - left < b:
            right += 1
        else:
            res += c * (right - left)
            break
    left = right
    index += 1

print(res + sum(ls[left:]))

Buggy python submission (verdict: Runtime Error):
import bisect

n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
res = 0
for b, c in ls2:
    if c <= ls[left]:
        break
    i = bisect.bisect(ls[left:], c)
    i = min(i, b)
    res += c * i
    left += i
print(res + sum(ls[left:]))

Fixed submission:
n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
right = 0
index = 0
res = 0
while left < n:
    if index == len(ls2):
        break
    b, c = ls2[index]
    if c <= ls[left]:
        break
    right = left + 1
    while right <= n:
        if right == n:
            res += c * (right - left)
            break
        if ls[right] <= c and right - left < b:
            right += 1
        else:
            res += c * (right - left)
            break
    left = right
    index += 1

print(res + sum(ls[left:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
res = 0
for b, c in ls2:
    i = bisect.bisect(ls[left:], c)
    i = min(i, b)
    res += c * i
    left += i
print(res + sum(ls[left:]))

Fixed submission:
n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
right = 0
index = 0
res = 0
while left < n:
    if index == len(ls2):
        break
    b, c = ls2[index]
    if c <= ls[left]:
        break
    right = left + 1
    while right <= n:
        if right == n:
            res += c * (right - left)
            break
        if ls[right] <= c and right - left < b:
            right += 1
        else:
            res += c * (right - left)
            break
    left = right
    index += 1

print(res + sum(ls[left:]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
for b, c in ls2:
    i = bisect.bisect(ls[left:], c)
    i = min(i, b)
    for j in range(i):
        ls[left + j] = c
    left += i
print(sum(ls))

Fixed submission:
n, m = map(int, input().split())
ls = sorted(map(int, input().split()))
ls2 = [list(map(int, input().split())) for _ in range(m)]
ls2.sort(key=lambda x: (x[1], x[0]), reverse=True)
left = 0
right = 0
index = 0
res = 0
while left < n:
    if index == len(ls2):
        break
    b, c = ls2[index]
    if c <= ls[left]:
        break
    right = left + 1
    while right <= n:
        if right == n:
            res += c * (right - left)
            break
        if ls[right] <= c and right - left < b:
            right += 1
        else:
            res += c * (right - left)
            break
    left = right
    index += 1

print(res + sum(ls[left:]))