Buggy python submission (verdict: Time Limit Exceeded):
from heapq import heapify, heappop, heappush
N,M = map(int,input().split())
A = list(map(int,input().split()))
heapify(A)
for i in range(M):
    B,C = map(int,input().split())
    for k in range(B):
        heappush(A,C)
        a = heappop(A)
        if a>=C:break
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort(reverse=True)
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)]
cnt = min(cnt,N)
for a in A:
  if cnt<N:
    ans.append(a)
    cnt+=1
  else:
    if cnt==N:
      cnt+=1
      ans.sort()
    if ans[0]<a:
      ans[0]=a
      ans.sort()
print(sum(ans))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
for _ in range(M):
  B,C = map(int,input().split())
  index = min(bisect.bisect_left(A,C), B)
  if index==0:
    continue
  A = [C for i in range(index)] + A[index:]
  A.sort()
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
for _ in range(M):
  B,C = map(int,input().split())
  index = min(bisect.bisect_left(A,C), B)
  if index==0:
    continue
  for i in range(index):
    A[i] = C
  A.sort()
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = []
for _ in range(M):
  B,C = map(int,input().split())
  BC.append((B,C))
BC.sort(key=lambda x: x[1], reverse=True)
for i in BC:
  b,c = i[0],i[1]
  index = bisect.bisect_left(A,c)
  index = min(index,b)
  if index==0:
    continue
  for j in range(index):
    A[j] = c
  A.sort()
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = []
for _ in range(M):
  B,C = map(int,input().split())
  BC.append((B,C))
BC.sort(key=lambda x: x[1], reverse=True)
for i in BC:
  b,c = i[0],i[1]
  index = bisect.bisect_left(A,c)
  index = min(index,b)
  if index==0:
    continue
  A = [c for _ in range(index)] + A[index:]
  A.sort()
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
BC = []
for _ in range(M):
  B,C = map(int,input().split())
  BC.append([B,C])
BC.sort(key=lambda x: x[1], reverse=True)
for i in BC:
  b,c = i[0],i[1]
  index = bisect.bisect_left(A,c)
  index = min(index,b)
  if index==0:
    continue
  A = [c for _ in range(index)] + A[index:]
  A.sort()
print(sum(A))

Fixed submission:
import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
D = []
for _ in range(M):
  D.append(list(map(int,input().split())))
D.sort(key=lambda x:x[1], reverse=True)
cnt = 0
ans = []
for d in D:
  b,c = d[0],d[1]
  ans += [c for _ in range(b)]
  cnt += b
  if cnt>=N:
    break
ans = ans[:min(cnt,N)] + A
ans.sort(reverse=True)
print(sum(ans[:N]))