Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(N)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))


Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Runtime Error):
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(N)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break

print(sum(A[:N][::-1]))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(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()))

res = []
for _ in range(M):
  B, C = map(int,input().split())
  res += [C]*B

X = A+res
X.sort(reverse = True)
print(sum(X[:N]))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(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()))
BC = [list(map(int,input().split())) for _ in range(M)]

res = []
for B, C in BC:
  for _ in range(B):
    res.append(C)

X = A+res
X.sort(reverse = True)
print(sum(X[:N]))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  B -= 1
  t = False
  for a in A[:B+1]:
    if a > C:
      B -= 1
      if B < 0:
        t = True
        break
  if t:
    continue
  A = A[B+1:] + [C]*(B+1)
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  B -= 1
  t = False
  while A[B] > C:
    B -= 1
    if B < 0:
      t = True
      break
  if t:
    continue
  if B == 0:
    A = A[1:] + [C]
  else:
    A = A[B+1:] + [C]*(B+1)
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

for B, C in BC:
  B -= 1
  t = False
  if A[0] > C:
    continue
  while A[B] > C:
    B -= 1
    if B < 0:
      t = True
      break
  if t:
    continue
  if B == 0:
    A = [C] + A[1:]
  else:
    A = [C]*len(A[:B+1]) + A[B+1:]
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

for B, C in BC:
  B -= 1
  t = False
  while A[B] > C:
    B -= 1
    if B < 0:
      t = True
      break
  if t:
    continue
  if B == 0:
    A = [C] + A[1:]
  else:
    A = [C]*len(A[:B+1]) + A[B+1:]
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]

for B, C in BC:
  B -= 1
  while A[B] > C and B >= 0:
    B -= 1
  if B == 0:
    A = [C] + A[1:]
  else:
    A = [C]*len(A[:B+1]) + A[B+1:]
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
A = sorted(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: x[1])

for B, C in BC:
  B -= 1
  while A[B] > C and B >= 0:
    B -= 1
  if B == 0:
    A = [C] + A[1:]
  else:
    A = [C]*len(A[:B+1]) + A[B+1:]
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int,input().split())
A = list(map(int,input().split()))
BC = [list(map(int,input().split())) for _ in range(M)]
BC.sort(key = lambda x: -x[1])

for B, C in BC:
  A.extend([C]*B)
  if len(A) > N*2:
    break
A.sort(reverse = True)
print(sum(A[:N]))