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

n, m = map(int,input().split())
a = list(map(int,input().split()))
heapq.heapify(a)
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        heapq.heappush(a,c)
        if min(a) > c:
            break
print(sum(heapq.nlargest(n, a)))

Fixed submission:
from collections import deque

n,m = map(int,input().split())
a = deque(sorted(list(map(int,input().split()))))
bc = [list(map(int,input().split()))[::-1] for i in range(m)]
bc.sort(reverse=True)
def hoge(n,m,a,bc):
    cur_sum = sum(a)
    for x in bc:
        c = x[0]
        for i in range(x[1]):
            prv_sum = cur_sum
            cur_sum -= a.popleft()
            cur_sum += c
            a.append(c)
            if prv_sum >= cur_sum:
                print(prv_sum)
                return
    print(cur_sum)
    return
hoge(n,m,a,bc)

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

n, m = map(int,input().split())
a = [(-x, x) for x in map(int,input().split())]
heapq.heapify(a)
sum_a = 0
for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        heapq.heappush(a,(-c, c))
for i in range(n):
    sum_a += heapq.heappop(a)[1]
print(sum_a)

Fixed submission:
from collections import deque

n,m = map(int,input().split())
a = deque(sorted(list(map(int,input().split()))))
bc = [list(map(int,input().split()))[::-1] for i in range(m)]
bc.sort(reverse=True)
def hoge(n,m,a,bc):
    cur_sum = sum(a)
    for x in bc:
        c = x[0]
        for i in range(x[1]):
            prv_sum = cur_sum
            cur_sum -= a.popleft()
            cur_sum += c
            a.append(c)
            if prv_sum >= cur_sum:
                print(prv_sum)
                return
    print(cur_sum)
    return
hoge(n,m,a,bc)

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

n,m = map(int, input().split())
a = list(map(int,input().split()))
a.sort()
for i in range(m):
    b,c = map(int,input().split())
    in_ind = bisect.bisect_left(a,c)
    if in_ind <= b:
        a = [c for i in range(in_ind)] + a[in_ind:]
    else:
        a = a[b:in_ind] + [c for i in range(b)] + a[in_ind:]
print(sum(a))

Fixed submission:
from collections import deque

n,m = map(int,input().split())
a = deque(sorted(list(map(int,input().split()))))
bc = [list(map(int,input().split()))[::-1] for i in range(m)]
bc.sort(reverse=True)
def hoge(n,m,a,bc):
    cur_sum = sum(a)
    for x in bc:
        c = x[0]
        for i in range(x[1]):
            prv_sum = cur_sum
            cur_sum -= a.popleft()
            cur_sum += c
            a.append(c)
            if prv_sum >= cur_sum:
                print(prv_sum)
                return
    print(cur_sum)
    return
hoge(n,m,a,bc)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
a = list(map(int,input().split()))
for i in range(m):
    b,c = map(int,input().split())
    a = a + [c for i in range(b)]
a.sort()
a.reverse()
print(sum(a[:n]))

Fixed submission:
from collections import deque

n,m = map(int,input().split())
a = deque(sorted(list(map(int,input().split()))))
bc = [list(map(int,input().split()))[::-1] for i in range(m)]
bc.sort(reverse=True)
def hoge(n,m,a,bc):
    cur_sum = sum(a)
    for x in bc:
        c = x[0]
        for i in range(x[1]):
            prv_sum = cur_sum
            cur_sum -= a.popleft()
            cur_sum += c
            a.append(c)
            if prv_sum >= cur_sum:
                print(prv_sum)
                return
    print(cur_sum)
    return
hoge(n,m,a,bc)