Buggy python submission (verdict: Time Limit Exceeded):
import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import Counter
    n, m = input_int_list()
    hq = list(Counter(input_int_list()).items())

    heapq.heapify(hq)

    for _ in range(m):
        b, c = input_int_list()
        while b > 0:
            k, v = heapq.heappop(hq)
            if k >= c:
                heapq.heappush(hq, (k, v))
                break
            else:
                if v == b:
                    heapq.heappush(hq, (c, b))
                    b = 0
                elif v > b:
                    heapq.heappush(hq, (c, b))
                    heapq.heappush(hq, (k, v - b))
                    b = 0
                elif b > v:
                    heapq.heappush(hq, (c, v))
                    b -= v
    ans = 0
    for k, v in hq:
        ans += k * v

    print(ans)

    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import Counter
    # 計算量 O(n*log_n + m*log_m)

    # 入力
    n, m = input_int_list()
    hq = list(Counter(input_int_list()).items())  # O(n)
    # 最小値管理にPriorityQueueを使う

    heapq.heapify(hq)  # O(n*log_n)

    queries = []
    for _ in range(m):
        queries.append(input_int_list())

    queries.sort(key=lambda x: x[1], reverse=True)  # O(m * log_m)

    # cが大きい数字から処理したほうが効率的になる
    for b, c in queries:
        while b > 0:
            k, v = heapq.heappop(hq)
            if k >= c:
                heapq.heappush(hq, (k, v))
                break
            else:
                if v == b:
                    heapq.heappush(hq, (c, b))
                    b = 0
                elif v > b:
                    heapq.heappush(hq, (c, b))
                    heapq.heappush(hq, (k, v - b))
                    b = 0
                elif b > v:
                    heapq.heappush(hq, (c, v))
                    b -= v
    ans = 0
    for k, v in hq:
        ans += k * v

    print(ans)

    return


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import defaultdict
    n, m = input_int_list()
    A = sorted(input_int_list())
    d = defaultdict(int)
    for a in A:
        d[a] += 1
    for _ in range(m):
        b, c = input_int_list()
        for key in sorted(d.keys()):
            if key >= c or b == 0:
                break
            else:
                if d[key] >= b:
                    tmp = b
                    d[key] -= tmp
                    b -= tmp
                    d[c] += tmp
                else:
                    d[c] += d[key]
                    b -= d[key]
                    d[key] = 0
    ans = 0
    for k, v in d.items():
        ans += k * v

    print(ans)

    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys
# sys.setrecursionlimit(100000)
import heapq


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import Counter
    # 計算量 O(n*log_n + m*log_m)

    # 入力
    n, m = input_int_list()
    hq = list(Counter(input_int_list()).items())  # O(n)
    # 最小値管理にPriorityQueueを使う

    heapq.heapify(hq)  # O(n*log_n)

    queries = []
    for _ in range(m):
        queries.append(input_int_list())

    queries.sort(key=lambda x: x[1], reverse=True)  # O(m * log_m)

    # cが大きい数字から処理したほうが効率的になる
    for b, c in queries:
        while b > 0:
            k, v = heapq.heappop(hq)
            if k >= c:
                heapq.heappush(hq, (k, v))
                break
            else:
                if v == b:
                    heapq.heappush(hq, (c, b))
                    b = 0
                elif v > b:
                    heapq.heappush(hq, (c, b))
                    heapq.heappush(hq, (k, v - b))
                    b = 0
                elif b > v:
                    heapq.heappush(hq, (c, v))
                    b -= v
    ans = 0
    for k, v in hq:
        ans += k * v

    print(ans)

    return


if __name__ == "__main__":
    main()