Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
nums=list(map(int,input().split()))
nums.sort(reverse=True)
changers=[]
for i in range(m):
  a,b=map(int,input().split())
  for j in range(a):
    changers.append(b)
changers.sort()
ans=0
for i in range(n):
  if changers[-i] == None:
    ans+=nums[-i]
  ans+=max(nums[-i],changers[-i])
print(ans)


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
nums=[]
for i in range(n):
  nums.append(int(input()))
nums.sort(reverse=True)
changers=[]
for i in range(m):
  changers.append(list(map(int,input().split())))
changers.sort(key=lambda x: x[1])
ans=0
for i in range(n):
  if changers[-i] == None:
    ans+=nums[-i]
  ans+=max(nums[-i],changers[-i])
print(ans)


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
nums=[int(input()) for i in range(n)]
nums.sort(reverse=True)
changers=[list(map(int,input().split())) for i in range(m)]
changers.sort(key=lambda x: x[1])
ans=0
for i in range(n):
  ans+=max(nums[-i],changers[-i])
print(ans)


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()

for i in range (m):
  b,c=map(int,input().split())
  d=0
  flag = 0
  while flag == 0 :
    a[d]=c
    d+=1
    b-=1
    if b!=0 or c <= a[d]:
      flag = 1
    a.sort()
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()

for i in range (m):
  b,c=map(int,input().split())
  d=0
  flag = 0
  while flag == 0 :
    a[d]=c
    d+=1
    b-=1
    if b!=0 or c <= a[d]:
      flag = 1
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
for i in range (m):
  b,c=map(int,input().split())
  d=0
  while b!=0 orc<=a[d] :
    a[d]=c
    d+=1
    b-=1
print(sum(a))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
for i in range (m):
  b,c=map(int,input().split())
  d=0
  while b!=0 orc<=a[d] :
    a[d]=c
    d+=1
    
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
changer=[]
cl=0
flag=0
for i in range(m):
  b=list(map(int,input().split()))
  changer.append(b)
changer.sort(key=lambda x:x[1],reverse=True)
for i in range(m):
  if flag==1:
    break
  for j in range(cl,min(cl+changer[i][0],n)):
    if a[j]<changer[i][1]:
      a[j]=changer[i][1]
    else:
      flag=1
      break
  cl+=changer[i][0]
print(sum(a))