Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    cand.append(c)
cand.sort()
cand = cand[::-1]
chan = []
for i in range(min(len(List),len(cand))):
  if cand[i] >=List[i]:
    chan.append(cand[i])
  else:
    break
leng = len(chan)
ans = chan +List[leng:]
print(sum(ans))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
List = sorted(list(map(int, input().split())))
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    cand.append(c)
cand = sorted(cand)[::-1]
chan = []
for i in range(min(len(List),len(cand))):
  if cand[i] >=List[i]:
    chan.append(cand[i])
  else:
    break
leng = len(chan)
ans = chan +List[leng:]
print(sum(ans))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
A = sorted(list(map(int, input().split())))
for _ in range(m):
  b,c = map(int,input().split())
  for i in range(b):
    if A[i] < c:
      A[i] = c
  A.sort()
print(sum(A))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
CardList = list(map(int,input().split()))

for _ in range(M):
  B, C = map(int,input().split())
  for _ in range(B):
    CardList.append(C)
CardList = sorted(CardList)
FinalList = CardList[-N:]

print(sum(FinalList))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
CardList = list(map(int,input().split()))

for _ in range(M):
  B, C = map(int,input().split())
  for _ in range(B):
    CardList.append(C)
CardList = sorted(CardList)
FinalList = CardList[-N:]

print(sum(FinalList))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
CardList = sorted(list(map(int,input().split())))
#print(CardList)
for _ in range(M):
  B, C = map(int,input().split())
  for i in range(B):
    if C > CardList[i]:
      CardList[i] = C
  CardList = sorted(CardList)
  
print(sum(CardList))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int,input().split())
CardList = sorted(list(map(int,input().split())))
#print(CardList)
for _ in range(M):
  B, C = map(int,input().split())
  for i in range(B):
    if C > CardList[i]:
      CardList[i] = C
  CardList = sorted(CardList)
  
print(sum(CardList))

Fixed submission:
n,m = map(int, input().split())
List = list(map(int, input().split()))
List.sort()
cand = []
for _ in range(m):
  b,c = map(int,input().split())
  cand.append((b,c))
bc = sorted(cand, key=lambda x: x[1], reverse=True)
i = 0
for b, c in bc:
    while b:
        if i < n and List[i] < c:
            List[i] = c
            b -= 1
        else:
            break
        i += 1
print(sum(List))