Buggy python submission (verdict: Time Limit Exceeded):

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    for i in range(m):
        b, c = list(map(int, input().split()))
        a += [c] * b
            
    a.sort(reverse=True)

    s = 0
    for i in range(n):
        s += a[i]
    
    print(s)


Fixed submission:

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = []
    for i in range(m):
        bc.append(list(map(int, input().split())))

    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()

    bcn = 0
    for i in range(n):
        if(a[i] < bc[bcn][1]):
            a[i] = bc[bcn][1]
            bc[bcn][0] -= 1
            if(bc[bcn][0] == 0):
                bcn += 1
                if(bcn == m):
                    break

    print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort()
    for i in range(m):
        b, c = list(map(int, input().split()))
        for j in range(b):
            if(a[j] < c):
                a[j] = c
            else:
                break
        a.sort()

    print(sum(a))


Fixed submission:

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = []
    for i in range(m):
        bc.append(list(map(int, input().split())))

    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()

    bcn = 0
    for i in range(n):
        if(a[i] < bc[bcn][1]):
            a[i] = bc[bcn][1]
            bc[bcn][0] -= 1
            if(bc[bcn][0] == 0):
                bcn += 1
                if(bcn == m):
                    break

    print(sum(a))

Buggy python submission (verdict: Runtime Error):

if __name__ == '__main__':
    n,m,k = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort()
    for i in range(m):
        b, c = list(map(int, input().split()))
        for j in range(b):
            if(a[j] < c):
                a[j] = c
            else:
                break
        a.sort()

    print(sum(a))


Fixed submission:

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = []
    for i in range(m):
        bc.append(list(map(int, input().split())))

    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()

    bcn = 0
    for i in range(n):
        if(a[i] < bc[bcn][1]):
            a[i] = bc[bcn][1]
            bc[bcn][0] -= 1
            if(bc[bcn][0] == 0):
                bcn += 1
                if(bcn == m):
                    break

    print(sum(a))

Buggy python submission (verdict: Time Limit Exceeded):

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    a.sort()
    for i in range(m):
        b, c = list(map(int, input().split()))
        for j in range(b):
            if(a[j] < c):
                a[j] = c
        a.sort()

    print(sum(a))


Fixed submission:

if __name__ == '__main__':
    n,m = list(map(int, input().split()))
    a = list(map(int, input().split()))
    bc = []
    for i in range(m):
        bc.append(list(map(int, input().split())))

    bc.sort(key=lambda x: x[1], reverse=True)
    a.sort()

    bcn = 0
    for i in range(n):
        if(a[i] < bc[bcn][1]):
            a[i] = bc[bcn][1]
            bc[bcn][0] -= 1
            if(bc[bcn][0] == 0):
                bcn += 1
                if(bcn == m):
                    break

    print(sum(a))