Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  a.extend([c]*b)
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  a.extend([c]*b)
a.sort(reverse=True)
print(sum(a[:n]))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  d.extend([c]*b)
if n < len(d):
  d=d[:n]
d.sort(reverse=True)
for i in range(len(d)):
  for j in range(i,n):
    if a[j] < d[i]:
      a[j]=d[i]
      break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  d.extend([c]*b)
if n < len(d):
  d=d[:n]
d.sort(reverse=True)
for i in range(len(d)):
  for j in range(n):
    if a[j] < d[i]:
      a[j]=d[i]
      break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  d.extend([c]*b)
if n < len(d):
  d=d[:n]
d.sort()
d=d[::-1]
for i in range(len(d)):
  for j in range(n):
    if a[j] < d[i]:
      a[j]=d[i]
      break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
d=[]
for i in range(m):
  b,c = map(int,input().split()) 
  for j in range(b):
    d.append(c)
bc.sort(key=lambda x:x[1])
d=d[::-1]
for i in range(len(d)):
  for j in range(n):
    if a[j] < d[i]:
      a[j]=d[i]
      break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for _ in range(m)]
a.sort()
bc.sort(key=lambda x:x[1])
d=[]
for i in range(m):
  b,c = bc[i]
  for j in range(b):
    d.append(c)
d=d[::-1]
for i in range(len(d)):
  for j in range(n):
    if a[j] < d[i]:
      a[j]=d[i]
      break
print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[list(map(int,input().split())) for i in range(m)]
bc.sort(key=lambda x:x[1])
for b,c in bc[::-1]:
  a.extend([c]*b)
  if len(a)>2*n:
    break
a.sort(reverse=True)
print(sum(a[:n]))