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

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    for _ in range(M):
        b, c = map(int, input().split())
        A += [c] * b

    A = sorted(A, key=lambda x: -x)
    return sum(A[:N])


if __name__ == '__main__':
    print(main())


Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())

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

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    for _ in range(M):
        b, c = map(int, input().split())
        A += [c] * b

    A = sorted(A, key=lambda x: -x)
    return sum(A[:N])


if __name__ == '__main__':
    print(main())


Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Runtime Error):
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    for _ in range(M):
        b, c = map(int, input().split())
        A += [c] * b

    A = sorted(A, key=lambda x: -x)
    return sum(A[:N]


if __name__ == '__main__':
    print(main())


Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    for _ in range(M):
        B, C = map(int, input().split())
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())



Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())

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

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    A.sort()

    for _ in range(M):
        B, C = map(int, input().split())
        insert = bisect_left(A, C)
        n = min(B, insert)
        A = A[n:insert] + [C] * n + A[insert:]

    return sum(A)



if __name__ == '__main__':
    print(main())



Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    for a in map(int, input().split()):
        heappush(heap, a)

    for _ in range(M):
        B, C = map(int, input().split())
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
            else:
                heappush(heap, a)
                break

    return sum(heap)


if __name__ == '__main__':
    print(main())


Fixed submission:
import sys
from heapq import heappush, heappop

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    heap = []
    ans = 0
    for a in map(int, input().split()):
        heappush(heap, a)
        ans += a

    BC = []
    for _ in range(M):
        B, C = map(int, input().split())
        BC.append((B, C))

    BC = sorted(BC, key=lambda x: -x[1])
    for B, C in BC:
        for _ in range(B):
            a = heappop(heap)
            if a < C:
                heappush(heap, C)
                ans += C - a
            else:
                heappush(heap, a)
                break

    return ans


if __name__ == '__main__':
    print(main())