Buggy python submission (verdict: Runtime Error):
n , m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = []
for i in range(m):
    b, c = map(int, input().split())
    bc.append([b,c])
bc.sort(key = lambda x : -x[1])
box = []
for b, c in bc:
    box.extend([c]*b)

k = 0
for b , c in bc:
    for i in range(k ,min( k + b, n)):
        A[i] = max(A[i] , c)
    k += b

print(sum(a))


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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
n , m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = []
for i in range(m):
    b, c = map(int, input().split())
    bc.append([b,c])
bc.sort(key = lambda x : -x[1])
box = []
for b, c in bc:
    box.extend([c]*b)

for i in range(min(len(box), n)):
    a[i] = max(a[i], box[i])

print(sum(a))


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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
n , m = map(int , input().split())
A = list(map(int , input().split()))
B_C = []
b_sum = 0
for i in range(m):
    b , c = map(int , input().split())
    B_C.append([b , c])
    b_sum += b
A.sort()
B_C.sort(key = lambda x : -x[1])
k = 0
for b , c in B_C:
    for i in range(k , k + b):
        if i == len(A):
            break
        A[i] = max(A[i] , c)
        k += b
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
n , m = map(int , input().split())
A = list(map(int , input().split()))
B_C = []
b_sum = 0
for i in range(m):
    b , c = map(int , input().split())
    B_C.append([b , c])
    b_sum += b
A.sort()
B_C.sort(key = lambda(x) : -x[1])
k = 0
for b , c in B_C:
    for i in range(k , k + b):
        if i = len(A):
            break
        A[i] = max(A[i] , c)
        k += b
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))

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

from operator import itemgetter
bc.sort(key=itemgetter(1))
k = 0
c = bc[0][1]
b = bc[0][1]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k >= len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn > len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))

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

from operator import itemgetter
bc.sort(key=itemgetter(1))
k = 0
c = bc[0][1]
b = bc[0][1]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k >= len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn > len(bc):
            break
        b = bc[bn]
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
for i in range(m):
    b, c = map(int, input().split())
    for j in range(b):
        A[j] = max(A[j], c)
    A = sorted(A)
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = sorted(list(map(int, input().split())))
for i in range(m):
    b, c = map(int, input().split())
    for j in range(b):
        A[j] = max(a[j], c)
print(sum(A))

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

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

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))