Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = []
for m in range(M):
  B, C = map(int, input().split())
  BCs.append((B, C))
BCs.sort(key=lambda BC: -BC[1])
#print(BCs)

index = 0
for m in range(M):
  B, C = BCs[m]
  for i in range(B):
    if As[index] < C:
      As[index] = C
      index += 1
    else:
      break
print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = []
for m in range(M):
  B, C = map(int, input().split())
  BCs.append((B, C))
BCs.sort(key=lambda BC: -BC[1])
#print(BCs)

index = 0
for m in range(M):
  B, C = BCs[m]
  for i in range(B):
    if index >= N:
      break
    if As[index] < C:
      As[index] = C
      index += 1
    else:
      break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
for m in range(M):
  B, C = map(int, input().split())
  count = 0
  for i in range(B):
    if As[i] < C:
      count += 1
    else:
      break
  As[0:count] = []
  #print(As)
  index = N-count
  for i in range(N-count):
    if As[i] > C:
      index = i
      break
  As[index:index] = [C]*count
  #print(As)
print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = []
for m in range(M):
  B, C = map(int, input().split())
  BCs.append((B, C))
BCs.sort(key=lambda BC: -BC[1])
#print(BCs)

index = 0
for m in range(M):
  B, C = BCs[m]
  for i in range(B):
    if index >= N:
      break
    if As[index] < C:
      As[index] = C
      index += 1
    else:
      break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
for m in range(M):
  B, C = map(int, input().split())
  count = 0
  for i in range(B):
    if As[0] < C:
      count += 1
      As.pop(0)
  #print(As)
  index = N-count
  for i in range(N-count):
    if As[i] > C:
      index = i
      break
  As[index:index] = [C]*count
  #print(As)
print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = []
for m in range(M):
  B, C = map(int, input().split())
  BCs.append((B, C))
BCs.sort(key=lambda BC: -BC[1])
#print(BCs)

index = 0
for m in range(M):
  B, C = BCs[m]
  for i in range(B):
    if index >= N:
      break
    if As[index] < C:
      As[index] = C
      index += 1
    else:
      break
print(sum(As))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
As = list(map(int, input().split()))
for m in range(M):
  B, C = map(int, input().split())
  As.sort()
  for i in range(B):
    if As[i] < C:
      As[i] = C
print(sum(As))


Fixed submission:
N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort()
BCs = []
for m in range(M):
  B, C = map(int, input().split())
  BCs.append((B, C))
BCs.sort(key=lambda BC: -BC[1])
#print(BCs)

index = 0
for m in range(M):
  B, C = BCs[m]
  for i in range(B):
    if index >= N:
      break
    if As[index] < C:
      As[index] = C
      index += 1
    else:
      break
print(sum(As))