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

a = list(map(int,input().split()))

min_a = min(a)

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

bc = sorted(bc, key=lambda x: -x[1])

memo = []

limit = 0

for i in range(m):
    a = a + [bc[i][1]]*bc[i][0]
    limit += bc[i][0]
    if limit >= n or min_a >= bc[i][1]:
        break

a.sort(reverse=True)

ans = 0

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

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
count = 0
for i,j in ab:
    t += [j]*i
    count += i
    if n <= count:
        break

t.sort(reverse=True)
        
print(sum(t[:n]))

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

a = list(map(int,input().split()))

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

bc = sorted(bc, key=lambda x: -x[1])

memo = []

limit = 0

for i in range(m):
    memo = memo + [bc[i][1]]*bc[i][0]
    limit += bc[i][0]
    if limit >= n:
        break

memo = a+memo

memo.sort(reverse=True)

ans = 0

for i in range(n):
    ans += memo[i]
print(ans)
    

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
count = 0
for i,j in ab:
    t += [j]*i
    count += i
    if n <= count:
        break

t.sort(reverse=True)
        
print(sum(t[:n]))

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
res = []
x = t.pop(0)

for i, j in ab:
    #print(res, i, j)
    while j < x:
        res.append(x)
        if len(t):
            x = t.pop(0)
        else:
            x = 0
    for _ in range(i):
        res.append(j)
        if len(res) >= n:
            break
    if len(res) >= n:
        break

if len(res) < n:
    for i in range(n):
        res.append(x)
        x = t.pop(0)
        if len(res) >= n:
            break
        

print(sum(res[0:n]))

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
count = 0
for i,j in ab:
    t += [j]*i
    count += i
    if n <= count:
        break

t.sort(reverse=True)
        
print(sum(t[:n]))

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
res = []
x = t.pop(0)

for i, j in ab:
    #print(res, i, j)
    while j < x:
        res.append(x)
        if len(t):
            x = t.pop(0)
        else:
            x = 0
    for _ in range(i):
        res.append(j)
        if len(res) > n:
            break
    if len(res) > n:
            break

if len(res) < n:
    for i in range(n):
        res.append(x)
        x = t.pop(0)
        if len(res) >= n:
            break
        

print(sum(res[0:n]))

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
count = 0
for i,j in ab:
    t += [j]*i
    count += i
    if n <= count:
        break

t.sort(reverse=True)
        
print(sum(t[:n]))

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

t = list(map(int,input().split()))

for _ in range(m):
    a, b = (int(x) for x in input().split())
    for _ in range(a):
        t.append(b)
t.sort(reverse=True)

res = 0

for i in range(n):
    res += t[i]
print(res)

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

t = list(map(int,input().split()))
ab=[]
for _ in range(m):
    a, b = (int(x) for x in input().split())
    ab.append([a,b])

t.sort(reverse=True)
ab = sorted(ab, key=lambda x: -x[1])
count = 0
for i,j in ab:
    t += [j]*i
    count += i
    if n <= count:
        break

t.sort(reverse=True)
        
print(sum(t[:n]))