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


def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    for b, c in bc:
        idx = bisect_left(arr, c)

        if b >= idx:
            s += sum(arr[idx:])
            arr = [c] * idx
        else:
            s += sum(arr[idx:])
            arr = arr[b:idx] + [c] * b

        if not arr:
            break

    print(s + sum(arr))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

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


def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    for b, c in bc:
        idx = bisect_left(arr, c)

        if b >= idx:
            s += sum(arr[idx:])
            arr = [c] * idx
        else:
            s += sum(arr[idx:])
            arr = arr[b:idx] + [c] * b

        if not arr:
            break

    print(s + sum(arr))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

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


def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    for b, c in bc:
        idx = bisect_left(arr, c)

        if b >= idx:
            s += sum(arr[b:])
            arr = [c] * idx + arr[idx:b]
        else:
            s += sum(arr[idx:])
            arr = arr[b:idx] + [c] * b

    print(s + sum(arr))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

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


def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    for b, c in bc:
        idx = bisect_left(arr, c)

        if idx:
            if b >= idx:
                arr[:idx] = [c] * idx
            else:
                arr[:idx] = arr[b:idx] + [c] * b

    print(sum(arr))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))

    for i in range(m):
        b, c = map(int, input().split())
        arr += [c] * b

    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()

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

def main():
    n, m = map(int, input().split())
    arr = list(map(int, input().split()))
    arr.sort()

    for i in range(m):
        b, c = map(int, input().split())
        idx = bisect_left(arr, c)

        if b >= idx:
            arr[:idx] = [c] * idx
        else:
            arr[:idx] = arr[b:idx] + [c] * b

    print(sum(arr))


main()

Fixed submission:
def main():
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    s = 0

    bc = [list(map(int, input().split())) for _ in range(m)]
    bc.sort(key=lambda x: x[1], reverse=True)

    arr = []
    sum_b = 0
    for b, c in bc:
        arr += [c] * b
        sum_b += b
        if sum_b >= n:
            break

    arr += a
    arr.sort(reverse=True)
    print(sum(arr[:n]))


main()