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

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    _A = {}
    A = []

    for i in map(int, input().split()):
        if i in _A:
            _A[i] += 1
        else:
            _A[i] = 1

    for (num, c) in _A.items():
        heapq.heappush(A, (num, c))
    # B = [0] * N
    # C = [0] * N
    # print(A)

    for i in range(M):
        B, C = map(int, input().split())

        while B > 0:
            item = heapq.heappop(A)

            if item[0] < C:
                replace = min(item[1], B)
                B -= replace
                remain = item[1] - replace

                heapq.heappush(A, (C, replace))

                if remain > 0:
                    heapq.heappush(A, (item[0], remain))

                # print("")
                # print(A, B, replace)
            else:
                heapq.heappush(A, item)
                break

    ans = 0
    for a in A:
        ans += a[0] * a[1]
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import math
import heapq
import bisect
input = sys.stdin.readline

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    # A = []

    # for i in map(int, input().split()):
    #     bisect.insort(A, i)

    # for i in range(M):
    #     B, C = map(int, input().split())
    _A = {}
    A = []

    for i in map(int, input().split()):
        if i in _A:
            _A[i] += 1
        else:
            _A[i] = 1

    for (num, c) in _A.items():
        heapq.heappush(A, (num, c))
    # B = [0] * N
    # C = [0] * N
    # print(A)

    B = [0] * M
    C = [0] * M
    T = [None] * M
    for i in range(M):
        b, c = map(int, input().split())
        T[i] = (c, b)

    T = sorted(T, reverse=True)

    finish = False
    for i in range(M):
        C, B = T[i]

        while B > 0:
            item = heapq.heappop(A)

            if item[0] < C:
                replace = min(item[1], B)
                B -= replace
                remain = item[1] - replace

                heapq.heappush(A, (C, replace))

                if remain > 0:
                    heapq.heappush(A, (item[0], remain))

                # print("")
                # print(A, B, replace)
            else:
                heapq.heappush(A, item)
                finish = True
                break

        if finish is True:
            break

    ans = 0
    for a in A:
        ans += a[0] * a[1]
    print(ans)


if __name__ == '__main__':
    main()

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

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    A = []

    for i in map(int, input().split()):
        heapq.heappush(A, i)
    # B = [0] * N
    # C = [0] * N

    for i in range(M):
        B, C = map(int, input().split())

        for i in range(B):
            heapq.heappush(A, max(C, heapq.heappop(A)))

    print(sum(A))


if __name__ == '__main__':
    main()


Fixed submission:
import sys
import math
import heapq
import bisect
input = sys.stdin.readline

MOD = 10 ** 9 + 7


def main():
    N, M = map(int, input().split())
    # A = []

    # for i in map(int, input().split()):
    #     bisect.insort(A, i)

    # for i in range(M):
    #     B, C = map(int, input().split())
    _A = {}
    A = []

    for i in map(int, input().split()):
        if i in _A:
            _A[i] += 1
        else:
            _A[i] = 1

    for (num, c) in _A.items():
        heapq.heappush(A, (num, c))
    # B = [0] * N
    # C = [0] * N
    # print(A)

    B = [0] * M
    C = [0] * M
    T = [None] * M
    for i in range(M):
        b, c = map(int, input().split())
        T[i] = (c, b)

    T = sorted(T, reverse=True)

    finish = False
    for i in range(M):
        C, B = T[i]

        while B > 0:
            item = heapq.heappop(A)

            if item[0] < C:
                replace = min(item[1], B)
                B -= replace
                remain = item[1] - replace

                heapq.heappush(A, (C, replace))

                if remain > 0:
                    heapq.heappush(A, (item[0], remain))

                # print("")
                # print(A, B, replace)
            else:
                heapq.heappush(A, item)
                finish = True
                break

        if finish is True:
            break

    ans = 0
    for a in A:
        ans += a[0] * a[1]
    print(ans)


if __name__ == '__main__':
    main()