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

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    if len(cards) > minIndex:
        ans += sum(cards[minIndex:])

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

Fixed submission:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if minIndex < n and cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    if minIndex < n:
        ans += sum(cards[minIndex:])

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

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

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    if len(cards) - 1 < minIndex:
        ans += sum(cards[minIndex:])

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

Fixed submission:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if minIndex < n and cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    if minIndex < n:
        ans += sum(cards[minIndex:])

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

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

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    ans += sum(cards[minIndex:])

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

Fixed submission:
from sys import stdin

def main():
    n,m = [int(x) for x in stdin.readline().split()]
    cards = [int(x) for x in stdin.readline().split()]
    cards.sort()

    bcs = [tuple(map(int,x.split())) for x in stdin.readlines()]
    bcs.sort(key=lambda x:x[1], reverse=True)

    ans = 0
    minIndex = 0
    isFinished = False

    for b,c in bcs:

        for _ in range(b):
            if minIndex < n and cards[minIndex] < c:
                ans += c
                minIndex += 1
            else:
                isFinished = True
                break
        
        if isFinished:
            break

    if minIndex < n:
        ans += sum(cards[minIndex:])

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