Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush, heapify
from collections import deque
import bisect
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
A.sort()
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    idx = bisect.insort_left(A,C)
print(sum(A[-N:]))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush, heapify
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
S = []
for i in range(M):
  B,C = map(int, input().split())
  A.extend([C]*B)
A.sort()
print(sum(A[-N:]))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush, heapify
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
S = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    heappush(S,-C)
heapify(A)
i = 0
l = len(S)
while i < min(N,l):
  a = heappop(A)
  q = -1*heappop(S)
  if a < q:
    heappush(A,q)
    i += 1
  else:
    heappush(A,a)
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush, heapify
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
S = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    heappush(S,-C)
A.sort()
i = 0
l = len(S)
while i < min(N,l):
  q = -1*heappop(S)
  if A[i] < q:
    A[i] = q
    i += 1
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop, heappush, heapify
from collections import deque
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
S = []
for i in range(M):
  B,C = map(int, input().split())
  for j in range(B):
    heappush(S,-C)
A.sort()
i = 0
l = len(S)
while i < min(N,l):
  q = -1*heappop(S)
  if A[i] < q:
    A[i] = q
    i += 1
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush,heapify
from collections import deque
import bisect
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int (i) for i in input().split()]
A.sort()
D = []
P = 0
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
ans = sum(A)
for i in range(min(P,N)):
  if A[i] < D[i]:
    ans += D[i] - A[i]
  else:
    break
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush,heapify
from collections import deque
import bisect
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int (i) for i in input().split()]
A.sort()
D = []
P = 0
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
ans = sum(A)
for i in range(min(P,N)):
  if A[i] < D[i]:
    ans += D[i] - A[i]
  else:
    break
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush,heapify
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int (i) for i in input().split()]
heapify(A)
D = []
P = 0
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
for i in range(P):
  if A[0] < D[i]:
    x = heappop(A)
    heappush(A,D[i])
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush,heapify
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
A = [int (i) for i in input().split()]
heapify(A)
D = []
P = 0
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
for i in range(P):
  if A[0] < D[i]:
    x = heappop(A)
    heappush(A,D[i])
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heappop,heappush,heapify
from collections import deque
N, M = map(int, input().split())
A = [int (i) for i in input().split()]
heapify(A)
D = []
P = 0
for i in range(M):
  B,C = map(int, input().split())
  D.extend([C]*B)
  P += B
D.sort(reverse=True)
for i in range(P):
  if A[0] < D[i]:
    x = heappop(A)
    heappush(A,D[i])
  else:
    break
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

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.extend([c]*b)
A.sort(reverse=True)
ans = sum(A[:N])
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
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())
  A.extend([c]*b)
A.sort(reverse=True)
ans = sum(A[:N])
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
C = []
ans = sum(A)


for i in range(M):
  b, c = map(int, input().split())
  C.extend([c]*b)

C.sort(reverse=True)
if M > N:
  C = C[:N] #Aの数を超える分は不要

if A[0] >= C[0]:
    print(ans)
    exit()

k = len(C)
for i in range(len(C)):  
  if C[i] < A[i]:
    k = i
    break
ans = sum(C[:k]) + sum(A[k:])
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
C = []
ans = sum(A)


for i in range(M):
  b, c = map(int, input().split())
  for j in range(b):
    C.append(c)
C.sort(reverse=True)
if M > N:
  C = C[:N] #Aの数を超える分は不要

if A[0] >= C[0]:
    print(ans)
    exit()

k = len(C)
for i in range(len(C)):  
  if C[i] < A[i]:
    k = i
    break
ans = sum(C[:k]) + sum(A[k:])
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
C = []
ans = sum(A)


for i in range(M):
  b, c = map(int, input().split())
  for j in range(b):
    C.append(c)
C.sort(reverse=True)
if M > N:
  C = C[:N] #Aの数を超える分は不要

if A[0] >= C[0]:
    print(ans)
    exit()

k = len(C)
for i in range(len(C)):  
  if C[i] < A[i]:
    k = i
    break
ans = sum(C[:k]) + sum(A[k:])
print(ans)

Fixed submission:
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
table = []
for i in range(N):
  table.append((A[i],1))
for i in range(M):
  b,c = map(int, input().split())
  table.append((c,b))
table.sort()
count = 0
ans = 0
while True:
  c, b = table.pop()
  if b <= N - count:
    ans += c*b
    count += b
  else:
    ans += (N-count)*c
    break
print(ans)