Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
D = []
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)

import bisect
from collections import deque

D = sorted(D,reverse=True)
A = sorted(A)
A.extend(D[:N])
A = sorted(A,reverse=True)

print(sum(A[:N]))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
D = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    D.append(C)

import bisect
from collections import deque

D = sorted(D,reverse=True)
A = sorted(A)
A.extend(D[:N])
A = sorted(A,reverse=True)

print(sum(A[:N]))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
D = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    D.append(C)

import bisect
from collections import deque

D = sorted(D,reverse=True)
A = sorted(A)

for i in range(min(len(D),N)):
  if D[i]>A[i]:
    A[i] = D[i]
  else:
    break
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
D = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
  D.append(C)

import bisect
from collections import deque

D = sorted(D,reverse=True)
A = sorted(A)

for i in range(min(len(D),N)):
  if D[i]>A[i]:
    A[i] = D[i]
  else:
    break
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
D = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
  D.append(C)

import bisect
from collections import deque

D = sorted(D,reverse=True)
A = sorted(A)

for i in range(min(len(D),N)):
  if D[i]>A[i]:
    A[i] = D[i]
  else:
    break
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
  B[i],C[i] = map(int, input().split())

import bisect
from collections import deque

for i in range(M):
  A = sorted(A)
  lm = bisect.bisect_right(A, C[i]-1)-1
  A = deque(A)
  for j in range(min(B[i],lm+1)):
    A.popleft()
  for j in range(min(B[i],lm+1)):
    A.appendleft(C[i])
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
  B[i],C[i] = map(int, input().split())

import bisect
from collections import deque

for i in range(M):
  A = sorted(A)
  lm = bisect.bisect_right(A, C[i]-1)-1
  A = deque(A)
  for j in range(min(B[i],lm+1)):
    A.popleft()
  for j in range(min(B[i],lm+1)):
    A.appendleft(C[i])
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
  B[i],C[i] = map(int, input().split())

import bisect
from collections import deque

for i in range(M):
  A = sorted(A)
  lm = bisect.bisect_right(A, C[i]-1)-1
  for j in range(min(B[i],lm+1)):
    A[j] = C[i]
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
B[i],C[i] = map(int, input().split())
import bisect
from collections import deque

for i in range(M):
  A = sorted(A)
  lm = bisect.bisect_right(A, C[i]-1)-1
  print(lm)
  for j in range(min(B[i],lm+1)):
    A[j] = C[i]
    
print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
B[i],C[i] = map(int, input().split())
import bisect
import deque

for i in range(M):
  A = sorted(A)
  lm = bisect.bisect_right(A, C[i]-1)-1
  print(lm)
  for j in range(min(B[i],lm+1)):
    A[j] = C[i]
    
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(M)]
C = [0 for i in range(M)]
for i in range(M):
B[i],C[i] = map(int, input().split())
import bisect
import deque

for i in range(M):
  A = deque(sorted(A))
  lm = A.bisect_right(A, C[i]-1)
  A[:min(B[i],lm)] = C[i]

print(sum(A))


Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [[] for _ in range(M)]
for i in range(M):
  BC[i] = list(map(int, input().split()))

BC.sort(key=lambda x: x[1], reverse=True)

for i in BC:
  A.extend([i[1]]*i[0])
  if len(A) > 2*N:
    break
    
A = sorted(A,reverse=True)
print(sum(A[:N]))