Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = [list(map(int, input().split())) for i in range(M)]

    A.sort()
    BC.sort(key=lambda x: x[1], reverse=True)
    BC2 = []

    for bc in BC:
        for i in range(bc[0]):
            BC2.append(bc[1])
    
    BC_idx = 0
    for i in range(N):
        if BC_idx >= len(BC2):
            break
        if A[i] < BC2[BC_idx]:
            A[i] = BC2[BC_idx]
            BC_idx += 1

    print(sum(A))

if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = [list(map(int, input().split())) for i in range(M)]

    A.sort()
    BC.sort(key=lambda x: x[1], reverse=True)
    BC2 = []

    for bc in BC:
        for i in range(bc[0]):
            BC2.append(bc[1])
    
    BC_idx = 0
    for i in range(N):
        if BC_idx >= len(BC2):
            break
        if A[i] < BC2[BC_idx]:
            A[i] = BC2[BC_idx]
            BC_idx += 1

    print(sum(A))

if __name__ == '__main__':
    solve()

Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    heapq.heapify(A)
    choice = [list(map(int, input().split())) for i in range(M)]

    for c in choice:
        for i in range(c[0]):
            pop = heapq.heappop(A)
            if pop < c[1]:
                heapq.heappush(A, c[1])
            else:
                heapq.heappush(A, pop)
                break
    
    print(sum(A))

if __name__ == '__main__':
    solve()

Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
def solve():
    input = sys.stdin.readline

    N, M = map(int, input().rstrip('\n').split())
    A = list(map(int, input().rstrip('\n').split()))
    BC = []
    for i in range(M):
        tmp = list(map(int, input().rstrip('\n').split()))
        BC.append(tmp)
    BC.sort(reverse=True, key=lambda x: x[1])
        
    heapq.heapify(A)
    for i in range(M):
        for j in range(BC[i][0]):
            p = heapq.heappop(A)
            if BC[i][1] < p:
                heapq.heappush(A,p)
                break
            else:
                heapq.heappush(A, BC[i][1])
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
def solve():
    input = sys.stdin.readline

    N, M = map(int, input().rstrip('\n').split())
    A = list(map(int, input().rstrip('\n').split()))
    BC = []
    for i in range(M):
        tmp = list(map(int, input().rstrip('\n').split()))
        BC.append(tmp)
    BC.sort(reverse=True, key=lambda x: x[1])
        
    heapq.heapify(A)
    for i in range(M):
        for j in range(BC[i][0]):
            if BC[i][1] < A[0]:
                break
            heapq.heapreplace(A, BC[i][1])
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        tmp = list(map(int, input().split()))
        BC.append(tmp)
    BC.sort(reverse=True, key=lambda x: x[1])
        
    heapq.heapify(A)
    for i in range(M):
        for j in range(BC[i][0]):
            if BC[i][1] < A[0]:
                break
            heapq.heapreplace(A, BC[i][1])
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        tmp = list(map(int, input().split()))
        BC.append(tmp)
    BC.sort(reverse=True, key=lambda x: x[1])
        
    heapq.heapify(A)
    for i in range(M):
        for j in range(BC[i][0]):
            if BC[i][1] < A[0]:
                break
            heapq.heapreplace(A, BC[i][1])
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    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):
            if C < A[0]:
                break
            heapq.heapreplace(A, C)
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    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):
            if C < A[0]:
                break
            heapq.heapreplace(A, C)
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
def solve():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = []
    C = []
    for i in range(M):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
        
    heapq.heapify(A)
    for i in range(M):
        for j in range(B[i]):
            if C[i] < A[0]:
                break
            heapq.heapreplace(A, C[i])
    
    print(sum(A))


if __name__ == '__main__':
    solve()


Fixed submission:
def solve():
    N, M = map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    operate = []
    for _ in range(M):
        b, c = map(int,input().split())
        operate.append([b,c])
    operate.sort(reverse=True, key=lambda x:x[1])    

    rewrite = [0] * N
    idx = 0
    for b,c in operate:
        for i in range(b):
            rewrite[idx] = c
            idx += 1
            if idx == N:
                break
        if idx == N:
            break
    
    ans = 0
    for i in range(N):
        ans += max(A[i],rewrite[i])
    
    print(ans)

if __name__ == '__main__':
    solve()