Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
		except:
			break
print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

Buggy python submission (verdict: Runtime Error):
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		if As[index] < c:
			As[index] = c
			index += 1
print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int,input().split())

As = np.array(list(map(int,input().split())))
Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		minArg = As.argmin()
		if As[minArg] < Cs[j - 1]:
			As[minArg] = Cs[j - 1]
print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

N, M = map(int,input().split())

As = np.sort(list(map(int,input().split())), kind="quicksort")

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		if As[n] < Cs[j - 1]:
			As[n] = Cs[j - 1]
	As = np.sort(As, kind="quicksort")

print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

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

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

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		minIndex = As.index(min(As))
		if As[minIndex] < Cs[j - 1]:
			As[minIndex] = Cs[j - 1]
print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

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

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

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		if As[n] < Cs[j - 1]:
			As[n] = Cs[j - 1]
	As.sort()

print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

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

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

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		if As[n] < Cs[j - 1]:
			As[n] = Cs[j - 1]
	As = sorted(As)

print(sum(As))

Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

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

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

Bs = []
Cs = []
for i in range(M):
	inputs = list(map(int,input().split()))
	Bs.append(inputs[0])
	Cs.append(inputs[1])

for j in range(1, M + 1):
	for n in range(Bs[j - 1]):
		if As[0] < Cs[j - 1]:
			As[0] = Cs[j - 1]
			As = sorted(As)

print(sum(As))


Fixed submission:
import numpy as np

N, M = map(int,input().split())

As = sorted(list(map(int,input().split())))
BCs = []

for i in range(M):
	inputs = list(map(int,input().split()))
	BCs.append(inputs)
BCs = sorted(BCs, reverse = True, key=lambda x: x[1])

index = 0
for bc in BCs:
	b = bc[0]
	c = bc[1]

	for n in range(b):
		try:
			if As[index] < c:
				As[index] = c
				index += 1
			else:
				break
		except:
			break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]

D = [C for B, C in opes for _ in range(B)]
D = sorted(D, reverse=True)[:N]

A.sort()
for i, d in enumerate(D):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x: -x[1])
D = [C for B, C in opes for _ in range(B)]

A.sort()
for i, d in enumerate(D):
    if i >= len(A) or A[i] >= d:
        break
    A[i] = d

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(reverse=True, key=lambda x: x[1])

count = lambda A, C: sum(a < C for a in A)

A.sort()
for B, C in opes:
    if A[0] >= C:
        break
    li = [C for _ in range(min(B, count(A, C)))]
    A = A[len(li):] + li

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]

count = lambda A, C: sum(a < C for a in A)

ope_dict = {}
for B, C in opes:
    if C in ope_dict.keys():
        ope_dict[C] = min(N, ope_dict[C]+B)
    else:
        ope_dict[C] = B
ope_dict = sorted(ope_dict.items(), reverse=True, key=lambda x:x[0])

A.sort()
for C, B in ope_dict:
    if A[0] >= C:
        break
    for _ in range(min(B, count(A, C))):
        A.pop(0)
        A.append(C)

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
_, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(reverse=True)

for B, C in opes:
    if min(A) > C:
        continue
    A.sort()
    for i in range(B):
        if A[i] < C:
            A[i] = C
        else:
            break

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
_, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(reverse=True)

for B, C in opes:
    if min(A) > C:
        continue
    A.sort()
    for i in range(B):
        A[i] = C if A[i] < C else A[i]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))

Buggy python submission (verdict: Time Limit Exceeded):
_, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]

for B, C in opes:
    A.sort()
    for i in range(B):
        A[i] = C if A[i] < C else A[i]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
opes = [tuple(map(int, input().split())) for _ in range(M)]
opes.sort(key=lambda x:-x[1])

D = []
lenD = 0
for B, C in opes:
    D += [C]*B
    lenD += B
    if lenD >= N:
        break

A.sort()
for i, d in enumerate(D[:N]):
    if A[i] >= d:
        break
    A[i] = d

print(sum(A))