Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
import collections

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    A = collections.deque(A)
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    Q = collections.deque()
    for b,c in B:
        for _ in range(b):
            Q.append(c)

    x = 0
    while Q:
        c = Q.pop()
        if len(A) > 0 and c > A[0]:
            A.popleft()
            x += c

    print(x + sum(A))

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    m = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,lo=m,hi=min(m+b,len(A)))
        x += (n-m)*c
        m = n

    print(x + sum(A[n:]))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,hi=min(b,len(A)))
        x += n*c
        A = A[n:]

    print(x + sum(A))

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    m = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,lo=m,hi=min(m+b,len(A)))
        x += (n-m)*c
        m = n

    print(x + sum(A[n:]))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,hi=b)
        x += n*c
        A = A[n:]

    print(x + sum(A))

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    m = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,lo=m,hi=min(m+b,len(A)))
        x += (n-m)*c
        m = n

    print(x + sum(A[n:]))

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input=sys.stdin.readline

def main():
    N,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    X = []
    while B:
        b,c = B.pop()
        n = 0
        for a in A:
            if b > n and a < c:
                X.append(c)
                n += 1
        A = A[n:]

    print(sum(X)+sum(A))

if __name__ == '__main__':
    main()


Fixed submission:
import sys
input=sys.stdin.readline
import bisect

def main():
    _,M= map(int,input().split())
    A = list(map(int,input().split()))
    A.sort()
    
    B = []
    for _ in range(M):
        b,c = map(int,input().split())
        B.append((b,c))
    B.sort(key=lambda x: x[1])

    x = 0
    m = 0
    while B:
        b,c = B.pop()
        n = bisect.bisect(A,c,lo=m,hi=min(m+b,len(A)))
        x += (n-m)*c
        m = n

    print(x + sum(A[n:]))

if __name__ == '__main__':
    main()