Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
a = [int(i) for i in readline().split()]
#bc = [[int(i) for i in readline().split()] for _ in range(m)]

import heapq

heapq.heapify(a)

for i in range(m):
#    b = bc[i][0]; c = bc[i][1]
    b,c = [int(i) for i in readline().split()]
    for _ in range(b):
        if a[0] < c:
            _ = heapq.heappop(a)
            heapq.heappush(a, c)
        else:
            break
    
ans = sum(a)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
A = [int(i) for i in readline().split()]

A.sort()

CB = []
for _ in range(m):
    b, c = [int(i) for i in readline().split()]
    CB.append([c, b])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(n):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= m:
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
a = [int(i) for i in readline().split()]
bc = [[int(i) for i in readline().split()] for _ in range(m)]

import heapq

heapq.heapify(a)

for i in range(m):
    b = bc[i][0]; c = bc[i][1]
    for _ in range(b):
        if a[0] < c:
            _ = heapq.heappop(a)
            heapq.heappush(a, c)
        else:
            break
    
ans = sum(a)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
A = [int(i) for i in readline().split()]

A.sort()

CB = []
for _ in range(m):
    b, c = [int(i) for i in readline().split()]
    CB.append([c, b])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(n):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= m:
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

#a = int(input())
n, m = map(int, input().split())
a = list(map(int, input().split()))
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
bc = [[int(i) for i in readline().split()] for _ in range(m)]

import heapq

heapq.heapify(a)

for i in range(m):
    b = bc[i][0]; c = bc[i][1]
    for _ in range(b):
        if a[0] < c:
            _ = heapq.heappop(a)
            heapq.heappush(a, c)
        else:
            break
    
ans = sum(a)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
A = [int(i) for i in readline().split()]

A.sort()

CB = []
for _ in range(m):
    b, c = [int(i) for i in readline().split()]
    CB.append([c, b])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(n):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= m:
        break

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

#a = int(input())
n, m = map(int, input().split())
a = list(map(int, input().split()))
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
bc = [[int(i) for i in readline().split()] for _ in range(m)]

import heapq

heapq.heapify(a)

for i in range(m):
    b = bc[i][0]; c = bc[i][1]
    for _ in range(b):
        if a[0] < c:
            _ = heapq.heappop(a)
            heapq.heappush(a, c)
        else:
            break
    
ans = sum(a)
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

n, m = map(int, input().split())
readline = sys.stdin.readline
A = [int(i) for i in readline().split()]

A.sort()

CB = []
for _ in range(m):
    b, c = [int(i) for i in readline().split()]
    CB.append([c, b])
    
CB.sort(reverse=True)

idx = 0
c = 0
for i in range(n):
    if A[i] < CB[idx][0]:
        A[i] = CB[idx][0]
        c += 1
    if CB[idx][1] <= c:
        idx += 1
        c = 0
    if idx >= m:
        break

print(sum(A))