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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import insort_left

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

d = []

for i in range(m):
    b,c = map(int,input().split())
    
    for j in range(b):
        d.append(c)        

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)


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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import insort_left

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

d = []

for i in range(m):
    b,c = map(int,input().split())
    
    for j in range(b):
        d.append(c)        

d.sort()
d.reverse()

#print(a)
#print(d)

for i in range(n):
    if i >= len(d):
        break

    if a[i] < d[i]:
        a[i] = d[i]
    else:
        break

#print(a)

sum = 0
for v in a:
    sum += v

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Runtime Error):
from bisect import insort_left

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

d = []

for i in range(m):
    b,c = map(int,input().split())
    
    for j in range(b):
        d.append(c)        

d.sort()
d.reverse()

#print(a)
#print(d)

for i in range(n):
    if a[i] < d[i]:
        a[i] = d[i]
    else:
        break

#print(a)

sum = 0
for v in a:
    sum += v

print(sum)


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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Runtime Error):
from bisect import insort_left

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

for i in range(m):
    b,c = map(int,input().split())

    for j in range(b+1):        
        if a[j] < c:
            a.remove(a[j])
            insort_left(a,c)
sum = 0
for v in a:
    sum += v

print(sum)


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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Runtime Error):
from bisect import insort

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

for i in range(m):
    b,c = map(int,input().split())

    for j in range(b+1):        
        if a[j] < c:
            a.remove(a[j])
            insort(a,c)

sum = 0
for v in a:
    sum += v

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

Buggy python submission (verdict: Runtime Error):
from bisect import insort

n,m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

#print(a)
for i in range(m):
    a.sort()
    b,c = map(int,input().split())

    for j in range(b+1):        
        if a[j] < c:
            a.remove(a[j])
            insort(a,c)

sum = 0
for v in a:
    sum += v

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

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

#print(a)
for i in range(m):
    a.sort()
    b,c = map(int,input().split())
    
    count = 0
    for j in range(len(a)):
        e = a[j]
        if count < b:
            if e < c:
                a[j] = c
                #print(c,end=",")
            #else:
                #print(e,end=",")
            count += 1
        else:
            break
        #else:
            #print(e,end=",")

    #print()

sum = 0
for v in a:
    sum += v

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)

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

#print(a)
for i in range(m):
    a.sort()
    b,c = map(int,input().split())
    
    count = 0
    for j in range(len(a)):
        e = a[j]
        if count < b:
            if e < c:
                a[j] = c
                #print(c,end=",")
            #else:
                #print(e,end=",")
            count += 1
        #else:
            #print(e,end=",")

    #print()

sum = 0
for v in a:
    sum += v

print(sum)

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

d = []

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

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

for elm in bc:
    for i in range(elm[0]):
        d.append(elm[1])
        if len(d) >= n:
            break
    if len(d) >= n:
        break

d.sort()
d.reverse()

#print(a)
#print(d)

sum = 0
ln = len(d)
for i in range(n):
    if i < ln:
        if a[i] < d[i]:
            sum += d[i]
        else:
            sum += a[i]
    else:
        sum += a[i]

print(sum)