Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
y = []
i=0
j=0
#print(a,x)
s=0
while len(y)<n:
    if (j == len(x)) or (a[i]>x[j][0]):
        y.append(a[i])
        s+=a[i]
        i+=1
    else:
        if len(y + [x[j][0]]*x[j][1]) > n:
            temp = n - len(y)
            y = y + [x[j][0]]*temp
            s = s + x[j][0]*temp
        else:
            y = y + [x[j][0]]*x[j][1]
            s = s + x[j][0]*x[j][1]
        #print(y)
        j+=1
y = y[:n]
print(s)

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
i=0
j=0
#print(a,x)
s=0
p = 0
x1 = len(x)
while p<n:
    if (j == x1) or (a[i]>x[j][0]):
        s+=a[i]
        p+=1
        i+=1
    else:
        if (p + x[j][1]) > n:
            temp = n - p
            p+=temp
            s = s + x[j][0]*temp
        else:
            p = p + x[j][1]
            s = s + x[j][0]*x[j][1]
        #print(y)
        j+=1
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
y = []
i=0
j=0
#print(a,x)
while len(y)<n:
    if (j == len(x)) or (a[i]>x[j][0]):
        y.append(a[i])
        i+=1
    else:
        y = y + [x[j][0]]*x[j][1]
        #print(y)
        j+=1
y = y[:n]
print(sum(y))

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
i=0
j=0
#print(a,x)
s=0
p = 0
x1 = len(x)
while p<n:
    if (j == x1) or (a[i]>x[j][0]):
        s+=a[i]
        p+=1
        i+=1
    else:
        if (p + x[j][1]) > n:
            temp = n - p
            p+=temp
            s = s + x[j][0]*temp
        else:
            p = p + x[j][1]
            s = s + x[j][0]*x[j][1]
        #print(y)
        j+=1
print(s)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
n,m = map(int,input().split())
a = list(map(int,input().split()))
for _ in range(m):
    b,c = map(int,input().split())
    a = a + [c]*b
a = Counter(a)
b = sorted(a.keys(),reverse = True)
x = 0
i = 0
s = 0
#print(b)
#print(a)
while x<n and i<len(b):
    if (x+a[b[i]]) > (n-1):
        temp = (n-x)
        x = x + temp
        s = s + b[i]*(temp)
        #print(temp,x,s)
        i+=1
    else:
        x = x + a[b[i]]
        s = s + b[i]*a[b[i]]
        #print(s,x)
        i+=1
print(s)

Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
i=0
j=0
#print(a,x)
s=0
p = 0
x1 = len(x)
while p<n:
    if (j == x1) or (a[i]>x[j][0]):
        s+=a[i]
        p+=1
        i+=1
    else:
        if (p + x[j][1]) > n:
            temp = n - p
            p+=temp
            s = s + x[j][0]*temp
        else:
            p = p + x[j][1]
            s = s + x[j][0]*x[j][1]
        #print(y)
        j+=1
print(s)

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 = a + [c]*b
a = sorted(a,reverse = True)
a = a[:n]
print(sum(a))


Fixed submission:
from operator import itemgetter
n,m = map(int,input().split())
a = list(map(int,input().split()))
a = sorted(a,reverse = True)
x = []
for _ in range(m):
    b,c = map(int,input().split())
    x.append([c,b])
x.sort(key = itemgetter(0),reverse = True)
i=0
j=0
#print(a,x)
s=0
p = 0
x1 = len(x)
while p<n:
    if (j == x1) or (a[i]>x[j][0]):
        s+=a[i]
        p+=1
        i+=1
    else:
        if (p + x[j][1]) > n:
            temp = n - p
            p+=temp
            s = s + x[j][0]*temp
        else:
            p = p + x[j][1]
            s = s + x[j][0]*x[j][1]
        #print(y)
        j+=1
print(s)