Buggy python submission (verdict: Time Limit Exceeded):
import heapq
[n,m]=[int(x) for x in input().split()]
a=[-int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

for item in bc:
  a=a+[-item[1],]*item[0]

heapq.heapify(a) 

ans=0
for i in range(n):
  ans=ans-heapq.heappop(a)

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
[n,m]=[int(x) for x in input().split()]
a=[-int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

for item in bc:
  a=a+[-item[1],]*item[0]

heapq.heapify(a) 

ans=0
for i in range(n):
  ans=ans-heapq.heappop(a)

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
b=[]
for item in bc:
  b=b+[-item[1],]*item[0]

heapq.heapify(b) 

ans=0
for i in range(n):
  if len(b)==0:
    ans=ans+sum(a[i:])
    break
  else:
    tmp=-heapq.heappop(b)
    if a[i]<tmp:
      ans=ans+tmp
    else:
      ans=ans+sum(a[i:])
      break

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
import heapq
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
b=[]
for item in bc:
  b=b+[-item[1],]*item[0]

heapq.heapify(b) 

ans=0
for i in range(n):
  if len(b)==0:
    ans=ans+sum(a[i:])
    break
  else:
    tmp=-heapq.heappop(b)
    if a[i]<tmp:
      ans=ans+tmp
    else:
      ans=ans+sum(a[i:])
      break

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
bc.sort(key=lambda x:x[1])
b=[]

for item in bc[::-1]:
  b=b+[item[1],]*item[0]
ans=0
len_b=len(b)
#print(a,b,bc)
for i in range(min(len_b,n)):
  if a[i]<b[i]:
    ans=ans+b[i]
  else:
    ans=ans+a[i]
if len_b<n:
  ans=ans+sum(a[len_b:])
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Runtime Error):
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
bc.sort(key=lambda x:x[1])
b=[]
for item in bc[::-1]:
  b=b+[item[1],]*item[0]
ans=0
#print(a,b,bc)
for i in range(n):
  if a[i]<b[i]:
    ans=ans+b[i]
  elif i!=n-1:
    ans=ans+sum(a[i:])
    break
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Runtime Error):
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x) for x in input().split()] for _ in range(m)]

a.sort()
bc.sort(key=lambda x:x[1])
b=[]
for item in bc[::-1]:
  b=b+[item[1],]*item[0]
ans=0
print(a,b,bc)
for i in range(n):
  if a[i]<b[i]:
    ans=ans+b[i]
  elif i!=n-1:
    ans=ans+sum(a[i:])
    break
print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)

Buggy python submission (verdict: Runtime Error):
[n,m]=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
bc=[[int(x)-1 for x in input().split()] for _ in range(m)]

a.sort()
bc.sort(key=lambda x:x[1])
b=[]
for item in bc[::-1]:
  b=b+[item[1],]*item[0]
ans=0
for i in range(n):
  if a[i]<b[i]:
    ans=ans+b[i]
  elif i!=n-1:
    ans=ans+sum(a[i+1:])
    break

print(ans)


Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
from operator import itemgetter
 
N,M = map(int,readline().split())
a = list(map(int,readline().split()))
m = map(int,read().split())
bc = list(zip(m,m))


#[n,m]=[int(x) for x in input().split()]
#a=[int(x) for x in input().split()]
#bc=[[int(x) for x in input().split()] for _ in range(m)]

bc.sort(key = itemgetter(1),reverse=True)
bc.append((N,-1))
change=[]
for b,c in bc:
    change += [c] * b
    if len(change) > N:
        break
 
a += change
a.sort()
answer = sum(a[-N:])
print(answer)