Buggy python submission (verdict: Runtime Error):
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

sumal = [0]
suma = 0
for a in al:
    suma += a
    sumal.append(suma)
    
cbl = sorted(cbl, reverse=True)

topn = [0 for i in range(n)]
pos = 0
for c,b in cbl:
    for _ in range(b):
        topn[pos] = c
        pos += 1
        if pos == n-1:
            break
    else:
        continue
    break

sumtopn = [0]
sumtop = 0
for t in topn:
    sumtop += t
    sumtopn.append(sumtop)
ans = 0
for i in range(n+1):
    left = sumal[-1] - sumal[i]
    right = sumtopn[i]
    tmp = left + right
    if tmp > ans:
        ans = tmp
        
print(ans)
    

Fixed submission:
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

sumal = [0]
suma = 0
for a in al:
    suma += a
    sumal.append(suma)
    
cbl = sorted(cbl, reverse=True)

topn = [0 for i in range(n)]
pos = 0
for c,b in cbl:
    for _ in range(b):
        topn[pos] = c
        pos += 1
        if pos == n:
            break
    else:
        continue
    break

sumtopn = [0]
sumtop = 0
for t in topn:
    sumtop += t
    sumtopn.append(sumtop)
ans = 0
for i in range(n+1):
    left = sumal[-1] - sumal[i]
    right = sumtopn[i]
    tmp = left + right
    if tmp > ans:
        ans = tmp
        
print(ans)

Buggy python submission (verdict: Runtime Error):
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

sumal = [0]
suma = 0
for a in al:
    suma += a
    sumal.append(suma)
    
cbl = sorted(cbl, reverse=True)

topn = [0 for i in range(n)]
pos = 0
for c,b in cbl:
    for _ in range(b):
        topn[pos] = c
        pos += 1
        if pos == n-1:
            break
    else:
        continue
    break

sumtopn = [0]
sumtop = 0
for t in topn:
    sumtop += t
    sumtopn.append(sumtop)
ans = 0
for i in range(n):
    left = sumal[-1] - sumal[i]
    right = sumtopn[i]
    tmp = left + right
    if tmp > ans:
        ans = tmp
        
print(ans)
    

Fixed submission:
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

sumal = [0]
suma = 0
for a in al:
    suma += a
    sumal.append(suma)
    
cbl = sorted(cbl, reverse=True)

topn = [0 for i in range(n)]
pos = 0
for c,b in cbl:
    for _ in range(b):
        topn[pos] = c
        pos += 1
        if pos == n:
            break
    else:
        continue
    break

sumtopn = [0]
sumtop = 0
for t in topn:
    sumtop += t
    sumtopn.append(sumtop)
ans = 0
for i in range(n+1):
    left = sumal[-1] - sumal[i]
    right = sumtopn[i]
    tmp = left + right
    if tmp > ans:
        ans = tmp
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

cbl = sorted(cbl)
for c,b in cbl:
    target = bisect.bisect_left(al, c)
    num = min(bisect.bisect_left(al, c), b)
    al = al[num:target] + [c] * num + al[target:]
print(sum(al))
    

Fixed submission:
import bisect
n,m = map(int,input().split())

al = list(map(int,input().split()))
al = sorted(al)


cbl = []
for _ in range(m):
    b,c = map(int,input().split())
    cbl.append((c,b))

sumal = [0]
suma = 0
for a in al:
    suma += a
    sumal.append(suma)
    
cbl = sorted(cbl, reverse=True)

topn = [0 for i in range(n)]
pos = 0
for c,b in cbl:
    for _ in range(b):
        topn[pos] = c
        pos += 1
        if pos == n:
            break
    else:
        continue
    break

sumtopn = [0]
sumtop = 0
for t in topn:
    sumtop += t
    sumtopn.append(sumtop)
ans = 0
for i in range(n+1):
    left = sumal[-1] - sumal[i]
    right = sumtopn[i]
    tmp = left + right
    if tmp > ans:
        ans = tmp
        
print(ans)