Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    asorted = sorted(alist)

    bc = sorted(bc, key=itemgetter(1), reverse=True)

    # print(f'initial: {asorted}')
    # print(f'bc: {bc}')

    fixed = []
    for b, c in bc:
        pick = [a for a in asorted if a >= c]

        if len(pick) > 0:
            asorted = asorted[:-len(pick)]
            fixed += pick

        if len(asorted) < 1:
            break

        for _ in range(b):
            if len(asorted) < 1:
                break
            asorted.pop(0)
            fixed.insert(0, c)

        # print(f'--- step n / {len(bc)} ---')
        # print(f'asorted: {asorted}')
        # print(f'pick   : {pick}')
        # print(f'fixed  : {fixed}')

    fixed += asorted

    print(sum(fixed))


main()

Fixed submission:
from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    xy = [(1, ai) for ai in alist] + bc
    rest = n
    total = 0
    for number, score in sorted(xy, key=itemgetter(1), reverse=True):
        if rest <= number:
            total += score * rest
            break
        else:
            total += score * number
            rest -= number

    print(total)


main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    asorted = sorted(alist)

    bc = sorted(bc, key=itemgetter(1))

    for b, c in bc:
        candidate = asorted[0:b]
        if candidate[-1] < c:
            for i in range(b):
                asorted[i] = c
            asorted = sorted(asorted)

    print(sum(asorted))


main()

Fixed submission:
from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    xy = [(1, ai) for ai in alist] + bc
    rest = n
    total = 0
    for number, score in sorted(xy, key=itemgetter(1), reverse=True):
        if rest <= number:
            total += score * rest
            break
        else:
            total += score * number
            rest -= number

    print(total)


main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    asorted = sorted(alist)

    for b, c in bc:
        candidate = asorted[0:b]
        if candidate[-1] < c:
            for i in range(b):
                asorted[i] = c
            asorted = sorted(asorted)

    print(sum(asorted))


main()

Fixed submission:
from operator import itemgetter


def main():
    n, m = list(map(int, input().split()))
    alist = list(map(int, input().split()))
    bc = []
    for _ in range(m):
        b, c = list(map(int, input().split()))
        bc.append((b, c))

    xy = [(1, ai) for ai in alist] + bc
    rest = n
    total = 0
    for number, score in sorted(xy, key=itemgetter(1), reverse=True):
        if rest <= number:
            total += score * rest
            break
        else:
            total += score * number
            rest -= number

    print(total)


main()