Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
*q, = map(int,input().split())
l = []
for _ in range(m):
  b, c = map(int,input().split())
  l.extend([c]*b)
#print(l)
l.sort(reverse=True)
for i in l:
  q.extend([i])
  if len(q) > 2*n:
    break
q.sort(reverse=True)
print(sum(q[:n]))

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    l.append(c)
li = q+l
print(sum(sorted(li,reverse=True)[:n]))

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import heapq
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    l.append(c)
li = q+l
li = np.array(li)*(-1)
#print(li)

def heapsort(li):
  h = []
  for v in li:
    heapq.heappush(h,v)
  return [heapq.heappop(h) for k in range(len(q))]
print(np.sum(heapsort(li))*(-1))

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    l.append(c)
li = sorted(q+l,reverse=True)
#print(li)

ans = 0
for i in range(len(q)):
  ans += li[i]
print(ans)

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
*q, = sorted(map(int,input().split()))
#print(q)
l = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    l.append(c)
l = sorted(l,reverse=True)
#print(l)

ans = 0
if len(q) < len(l):
  for k in range(len(q)):
    if l[k]>q[k]:
      ans += l[k]
    else:
      ans += sum(q[k:])
      break
  print(ans)
  exit()

else:
  for j in range(len(l)):
    if q[j] > l[j]:
      ans += q[j]
      #print('ans',ans)
    else:
      ans += l[j]
      #print('ans',ans)
print(ans+sum(q[len(l):]))

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
*q, = sorted(map(int,input().split()))
#print(q)
l = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    l.append(c)
l = sorted(l,reverse=True)
#print(l)

ans = 0
for j in range(len(l)):
  if q[j] > l[j]:
    ans += q[j]
    #print('ans',ans)
  else:
    ans += l[j]
    #print('ans',ans)
print(ans+sum(q[len(l):]) if len(l)<len(q) else ans)

Fixed submission:
n,m=map(int,input().split())
*q, = map(int,input().split())
#print(q)
l = [list(map(int,input().split())) for _ in range(m)]
l.sort(key=lambda x:x[1], reverse=True)
#print(l)
for b,c in l:
  q.extend([c]*b)
  if len(q) > n*2:
    break

print(sum(sorted(q,reverse=True)[:n]))