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

for i in range(m):
    b, c = map(int, input().split())
    A += [c]*b

A.sort(reverse=True)
A = A[0:n]

print(sum(A))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

for i in range(m):
    b, c = map(int, input().split())
    A += [c]*b

A.sort(reverse=True)
A = A[0:n]

print(sum(A))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

B = [0]*n
for i in range(m):
    b, c = map(int, input().split())
    B += [c]*b

B.sort(reverse=True)
B = B[0:n]

import heapq
heapq.heapify(B)

for a in A:
    x = heapq.heappop(B)
    if a >= x:
        heapq.heappush(B, a)
    else:
        heapq.heappush(B, x)
print(sum(B))


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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

B = [0]*n
for i in range(m):
    b, c = map(int, input().split())
    B += [c]*b

B.sort(reverse=True)
B = B[0:n]

import heapq
heapq.heapify(B)

for a in A:
    x = heapq.heappop(B)
    if a >= x:
        heapq.heappush(B, a)
    else:
        heapq.heappush(B, x)
print(sum(B))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l[:b] = [c if x < c else x for x in l[:b]]
  l.sort()
  count += b
  if count >= n:
    break

print(sum(l))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

for i in range(m):
  b, c = map(int, input().split())
  l[:b] = [c if x < c else x for x in l[:b]]
  l.sort()

print(sum(l))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

for i in range(m):
  b, c = map(int, input().split())
  l[:b] = [c if x < c else x for x in l[:b]]
  l.sort()

print(sum(l))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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

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

for i in range(m):
  b, c = map(int, input().split())
  lp = l[:b]
  lp = [c if x < c else x for x in lp]
  l[:b] = lp
  l.sort()

print(sum(l))

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

bc = [list(map(int, input().split())) for i in range(m)]
bc = sorted(bc, key=lambda x: x[1], reverse=True)
count = 0

for i in range(m):
  b, c = bc[i][0], bc[i][1]
  l += [c]*b
  count += b
  if count >= n:
    break

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