Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
data=[[int(i) for i in input().split()] for i in range(m)]
data.sort(key=lambda x:x[1],reverse=True)
a.sort(reverse=True)
ans=0
flag=True
for _ in range(n):
  if data[0][1]<a[0] or flag==False:
    ans+=a.pop(0)
  else:
    ans+=data[0][1]
    data[0][0]-=1
    if data[0][0]==0:
      if len(data)>1:
        data=data[1:]
      else:
        flag=False
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=[[1,int(i)] for i in input().split()]
data=[[int(i) for i in input().split()] for i in range(m)]
data.extend(a)
data.sort(key=lambda x:x[1],reverse=True)
ans=0
flag=True
index=0
while n>0:
  tmp=min(data[index][0],n)
  ans+=data[index][1]*tmp
  n-=tmp
  index+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
data=[[int(i) for i in input().split()] for i in range(m)]
data.sort(key=lambda x:x[1],reverse=True)
a.sort(reverse=True)
ans=0
flag=True
for i in range(n):
  if data[0][1]<a[0] or flag==False:
    ans+=a[0]
    a=a[1:]
  else:
    ans+=data[0][1]
    data[0][0]-=1
    if data[0][0]==0:
      if len(data)>1:
        data=data[1:]
      else:
        flag=False
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=[[1,int(i)] for i in input().split()]
data=[[int(i) for i in input().split()] for i in range(m)]
data.extend(a)
data.sort(key=lambda x:x[1],reverse=True)
ans=0
flag=True
index=0
while n>0:
  tmp=min(data[index][0],n)
  ans+=data[index][1]*tmp
  n-=tmp
  index+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
data=[[int(i) for i in input().split()] for i in range(m)]
data.sort(key=lambda x:x[1])
data.reverse()
a.sort()
a.reverse()
ans=0
flag=True
for i in range(n):
  if data[0][1]<a[0] or flag==False:
    ans+=a[0]
    a=a[1:]
  else:
    ans+=data[0][1]
    data[0][0]-=1
    if data[0][0]==0:
      if len(data)>1:
        data=data[1:]
      else:
        flag=False
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=[[1,int(i)] for i in input().split()]
data=[[int(i) for i in input().split()] for i in range(m)]
data.extend(a)
data.sort(key=lambda x:x[1],reverse=True)
ans=0
flag=True
index=0
while n>0:
  tmp=min(data[index][0],n)
  ans+=data[index][1]*tmp
  n-=tmp
  index+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
a=list(map(int,input().split()))
data=[[int(i) for i in input().split()] for i in range(m)]
data.sort(key=lambda x:x[1])
data.reverse()
a.sort()
a.reverse()
ans=0
flag=True
for i in range(n):
  if flag==True:
    if data[0][1]<a[0]:
      ans+=a[0]
      a=a[1:]
    else:
      ans+=data[0][1]
      data[0][0]-=1
      if data[0][0]==0:
        if len(data)>1:
          data=data[1:]
        else:
          flag=False
  else:
    ans+=a[0]
    a=a[1:]
print(ans)

Fixed submission:
n,m=map(int,input().split())
a=[[1,int(i)] for i in input().split()]
data=[[int(i) for i in input().split()] for i in range(m)]
data.extend(a)
data.sort(key=lambda x:x[1],reverse=True)
ans=0
flag=True
index=0
while n>0:
  tmp=min(data[index][0],n)
  ans+=data[index][1]*tmp
  n-=tmp
  index+=1
print(ans)