Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int,input().split())
a = list(map(int,input().split()))
p = [[aa,1] for aa in a]
for _ in range(m):
  b = list(map(int,input().split()))
  b.reverse()
  p.append(b)
p.sort()
p.reverse()
rest = n
score = 0
for ni in range(n):
  use = min(p[0][1],rest)
  rest -= use
  score += p[0][0] * use
  del p[0]
  if rest==0: break
print(score)

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)

Buggy python submission (verdict: Time Limit Exceeded):
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [[AA[i],1] for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [[c,b]]
A.sort()
A.reverse()
rest = n
score = 0
while True:
  C = A[0][0]
  B = A[0][1]
  score += B * C
  rest -= B
  del A[0]
  if rest < 0:
    score += rest * C
    break
print(score)

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)

Buggy python submission (verdict: Time Limit Exceeded):
n,m= map(int,input().split())
def f(x):
  return [int(x),1]
A = list(map(f,input().split()))
for _ in range(m):
  b,c = map(int,input().split())
  A += [[c,b]]
A.sort()
A.reverse()
rest = n
score = 0
while True:
  C = A[0][0]
  B = A[0][1]
  score += B * C
  rest -= B
  del A[0]
  if rest < 0:
    score += rest * C
    break
print(score)

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)

Buggy python submission (verdict: Time Limit Exceeded):
n,m= map(int,input().split())
A = list(map(int,input().split()))
B = []
for _ in range(m):
  b,c = map(int,input().split())
  B += [c]*b
C = A + B
C.sort()
C.reverse()
print(sum(C[0:n]))

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)

Buggy python submission (verdict: Runtime Error):
n,m= map(int,input().split())
A = list(map(int,input().split()))
B = []
for _ in range(m):
  b,c = map(int,input().split())
  B += [c]*b
A.sort()
B.sort(reverse=True)
B += [0]*n
mark = 0
i = 0
while True:
  if A[i] > B[i]:
    mark = i
    break
  else:
    i += 1
print(sum(B[:i])+sum(A[i:n+1]))

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)

Buggy python submission (verdict: Time Limit Exceeded):
n,m= map(int,input().split())
A = list(map(int,input().split()))
B = []
for _ in range(m):
  b,c = map(int,input().split())
  B += [c]*b
A.sort()
B.sort(reverse=True)
B += [0]*n
c = 0
for i in range(n):
  c += max(A[i],B[i])
print(c)

Fixed submission:
n,m= map(int,input().split())
AA = list(map(int,input().split()))
A = [(AA[i],1) for i in range(n)]
for _ in range(m):
  b,c = map(int,input().split())
  A += [(c,b)]
A.sort()
A.reverse()
rest = n
score = 0
for (cc,bb) in A:
  use = min(bb,rest)
  rest -= use
  score += use*cc
print(score)