Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
memo = 10 **10
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        if count > N:
            if c >= memo:
                cb += [c] * b
        else:
            count += b
            cb += [c] * b
            memo = min(memo, c)

ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
memo = 10 **10
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        if count > N:
            if c >= memo:
                cb += [c] * b
        else:
            count += b
            cb += [c] * b
            memo = min(memo, c)

ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        count += b
        cb += [c] * b
ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo: #枝刈り
        A += [c] * b

A.sort(reverse = True)

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()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo:
        A += [c] * b

A.sort(reverse = True)

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()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
for _ in range(M):
    b, c = map(int, input().split())
    A += [c] * b

A.sort(reverse = True)

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()))

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1