Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
ansl = []
counter = 0
B = []
for i in range(M):
    BN,CN = map(int,input().split())
    B.append([CN,BN])
B.sort()
for i in A:
    for j in range(len(B)):
        if B[-1][0] > i:
            ansl += [B[-1][0]]*B[-1][1]
            counter += B[-1][1]
            B.pop(-1)
        else:
            break
    ansl.append(i)
    counter += 1
    if counter > N:
        break
ans = 0
for i in range(N):
    ans += ansl[i]
print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
counter = 0
ans = 0
B = []
for i in range(M):
    BN,CN = map(int,input().split())
    B.append([CN,BN])
B.sort()
for i in A:
    for j in range(len(B)):
        if B[-1][0] > i:
            if counter <= N - B[-1][1]:
                ans += B[-1][0] * B[-1][1]
                counter += B[-1][1]
                B.pop(-1)
            else:
                ans += B[-1][0] * (N - counter)
                counter = N
        else:
            break
    if counter == N:
        break
    else:
        counter += 1
        ans += i
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
ansl = []
counter = 0
B = []
for i in range(M):
    BN,CN = map(int,input().split())
    B.append([CN,BN])
B.sort()
for i in A:
    for j in range(len(B)):
        if B[-1][0] > i:
            ansl += [B[-1][0]]*B[-1][1]
            counter += B[-1][1]
            B.pop(-1)
        else:
            break
    ansl.append(i)
    counter += 1
    if counter > N:
        break
ans = 0
for i in range(N):
    ans += ansl[i]
print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
counter = 0
ans = 0
B = []
for i in range(M):
    BN,CN = map(int,input().split())
    B.append([CN,BN])
B.sort()
for i in A:
    for j in range(len(B)):
        if B[-1][0] > i:
            if counter <= N - B[-1][1]:
                ans += B[-1][0] * B[-1][1]
                counter += B[-1][1]
                B.pop(-1)
            else:
                ans += B[-1][0] * (N - counter)
                counter = N
        else:
            break
    if counter == N:
        break
    else:
        counter += 1
        ans += i
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
A = list(map(int,input().split()))
for i in range(M):
    B,C = map(int,input().split())
    A += [C] * B
A.sort()
A.reverse()
ans = 0
for i in range(N):
    ans += A[i]
print(ans)

Fixed submission:
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
A.reverse()
counter = 0
ans = 0
B = []
for i in range(M):
    BN,CN = map(int,input().split())
    B.append([CN,BN])
B.sort()
for i in A:
    for j in range(len(B)):
        if B[-1][0] > i:
            if counter <= N - B[-1][1]:
                ans += B[-1][0] * B[-1][1]
                counter += B[-1][1]
                B.pop(-1)
            else:
                ans += B[-1][0] * (N - counter)
                counter = N
        else:
            break
    if counter == N:
        break
    else:
        counter += 1
        ans += i
print(ans)