Buggy python submission (verdict: Runtime Error):
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        res += max(A[i], D[i])
        

    res = 0
    for i in range(N):
        if i < ld:
            res += max(A[i], D[i])
        else:
            res += A[i]
    

    print(res)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        if i < ld:
            res += max(A[i], D[i])
        else:
            res += A[i]
    

    print(res)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        res += max(A[i], D[i])
        

    res = 0
    for i in range(N):
        res += max(A[i], D[i])

    print(res)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        if i < ld:
            res += max(A[i], D[i])
        else:
            res += A[i]
    

    print(res)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    B, C = [], []
    D = [0] * N
    A.sort()
    for i in range(M):
        b, c = map(int, input().split())
        B.append(b)
        C.append(c)
        D += [c] * b
        D = sorted(D, reverse=True)[:N]

    res = 0
    for i in range(N):
        res += max(A[i], D[i])

    print(res)

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []

    D = []
    for i in range(M):
        b, c = map(int, input().split())
        BC.append((b, c))
    BC.sort(key=lambda x: x[1], reverse=True)
    ld = 0
    for b, c in BC:
        D += [c] * b
        ld += b
        if ld > N:
            break
    
    res = 0
    A.sort()
    for i in range(N):
        if i < ld:
            res += max(A[i], D[i])
        else:
            res += A[i]
    

    print(res)

if __name__ == "__main__":
    main()