Buggy python submission (verdict: Time Limit Exceeded):
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  AB = [ab[1] for ab in AB for i in range(ab[0])]
  
  index = min([N, len(AB)])
  result = 0
  for i in range(index):
    if AB[i] < C[i]:
      index = i
      break
    result += AB[i]
  
  if index < N:
    result += sum(C[index:])
  print(result)

if __name__ == "__main__":
  main()

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
# C と AB それぞれソートするのが遅い？そんなに？
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
#C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = [ab[1] for ab in AB for i in range(ab[0])]
#AB = sorted(AB, key=lambda x: x[1], reverse=True)
C.extend(AB)
C = sorted(C, reverse=True)
print(sum(C[:N]))

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = sorted(AB, key=lambda x: x[1], reverse=True)
AB = [ab[1] for ab in AB for i in range(ab[0])]

index = min([N, len(AB)])
result = 0
for i in range(index):
  if AB[i] < C[i]:
    index = i
    break
  result += AB[i]

if index < N:
  result += sum(C[index:])
print(result)

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = sorted(AB, key=lambda x: x[1], reverse=True)
AB = [ab[1] for ab in AB for i in range(ab[0])]

index = min([N, len(AB)])
for i in range(index):
  if AB[i] < C[i]:
    index = i
    break
result = sum(AB[:index])
if index < N:
  result += sum(C[index:])
print(result)

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = sorted(AB, key=lambda x: x[1], reverse=True)
AB = [ab[1] for ab in AB for i in range(ab[0])]

result = sum(C)
result += sum([AB[i] - C[i] for i in range(min([len(C), len(AB)])) if AB[i] > C[i]])
print(result)

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = sorted([ab[1] for ab in AB for i in range(ab[0])], reverse=True)

result = sum(C)
result += sum([AB[i] - C[i] for i in range(min([len(C), len(AB)])) if AB[i] > C[i]])
print(result)

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()

Buggy python submission (verdict: Time Limit Exceeded):
N, M = list(map(int, input().split()))
C = list(map(int, input().split()))
C = sorted(C)
AB = [list(map(int, input().split())) for i in range(M)]
AB = sorted([ab[1] for ab in AB for i in range(ab[0])], reverse=True)

result = sum(C)
for i in range(len(AB)):
  if i >= N or AB[i] <= C[i]:
    break
  result += AB[i] - C[i]
print(result)

Fixed submission:
def main():
  N, M = list(map(int, input().split()))
  C = list(map(int, input().split()))
  C = sorted(C)
  AB = [list(map(int, input().split())) for i in range(M)]
  AB = sorted(AB, key=lambda x: x[1], reverse=True)
  
  result = 0
  i, j = 0, 0
  while i < N:
    A, B = AB[j] if j < M else (0, 0)
    j += 1
    if i+A < N and B >= C[i+A]:
      i += A
      result += A * B
    else:
      break
    
  while i < N:
    if C[i] > B:
      result += sum(C[i:])
      break
    else:
      result += B
      A -= 1      
      if j < M and A == 0:
        A, B = AB[j] if j < M else (0, 0)
        j += 1
    
    i += 1
  
  print(result)

if __name__ == "__main__":
  main()