Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int, input().split())
A=list(map(int, input().split()))
A=sorted(A)

L=[[0,0] for _ in range(M)]
for i in range(M):
    b,c=map(int, input().split())
    L[i][0]=b
    L[i][1]=c

L=sorted(L,reverse=True, key=lambda x: x[1])
X=[]
for l in L:
    b=l[0]
    c=l[1]
    n=min(b,N-len(X))
    X=X+[c]*n
    if len(X)==N:
        break
#print(X)
ans=0
for i in range(N):
    if len(X)>i:
        ans+=max(X[i],A[i])
    else:
        ans+=A[i]

print(ans)

Fixed submission:
N, M=map(int, input().split())
A=list(map(int, input().split()))
bc=[]
for i in range(M):
  b, c=map(int, input().split())
  bc.append((c,b))
for i in range(N):
  bc.append((A[i], 1))
  
bc=sorted(bc, key=lambda x : x[0], reverse=True)

ans=0
res=N
for x in bc:
  c=x[0]
  b=min(x[1], res)
  ans+=c*b
  res-=b
  if res==0:
    break
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):

N,M=map(int, input().split())
A=list(map(int, input().split()))
A=sorted(A)

L=[]
for i in range(M):
    b,c=map(int, input().split())
    L.append([b,c])

L=sorted(L,reverse=True, key=lambda x: x[1])
X=[]
for l in L:
    b=l[0]
    c=l[1]
    n=min(b,N-len(X))
    X=X+[c]*n
    if len(X)==N:
        break
#print(X)
ans=0
for i in range(N):
    if len(X)>i:
        ans+=max(X[i],A[i])
    else:
        ans+=A[i]

print(ans)

Fixed submission:
N, M=map(int, input().split())
A=list(map(int, input().split()))
bc=[]
for i in range(M):
  b, c=map(int, input().split())
  bc.append((c,b))
for i in range(N):
  bc.append((A[i], 1))
  
bc=sorted(bc, key=lambda x : x[0], reverse=True)

ans=0
res=N
for x in bc:
  c=x[0]
  b=min(x[1], res)
  ans+=c*b
  res-=b
  if res==0:
    break
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import numpy as np
N,M=map(int, input().split())
A=list(map(int, input().split()))
A=sorted(A)

L=[]
for i in range(M):
    b,c=map(int, input().split())
    L.append([b,c])

L=sorted(L,reverse=True, key=lambda x: x[1])
X=[]
for l in L:
    b=l[0]
    c=l[1]
    n=min(b,N-len(X))
    X=X+[c]*n
    if len(X)==N:
        break
#print(X)
ans=0
for i in range(N):
    if len(X)>i:
        ans+=max(X[i],A[i])
    else:
        ans+=A[i]

print(ans)

Fixed submission:
N, M=map(int, input().split())
A=list(map(int, input().split()))
bc=[]
for i in range(M):
  b, c=map(int, input().split())
  bc.append((c,b))
for i in range(N):
  bc.append((A[i], 1))
  
bc=sorted(bc, key=lambda x : x[0], reverse=True)

ans=0
res=N
for x in bc:
  c=x[0]
  b=min(x[1], res)
  ans+=c*b
  res-=b
  if res==0:
    break
    
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect
import numpy as np
N,M=map(int, input().split())
A=list(map(int, input().split()))
A=np.array(sorted(A))
for i in range(M):
    b,c=map(int, input().split())
    x=bisect.bisect_left(A,c)
    x=min(x,b)
    A[:x]=c
    A=np.sort(A)
print(sum(A))


Fixed submission:
N, M=map(int, input().split())
A=list(map(int, input().split()))
bc=[]
for i in range(M):
  b, c=map(int, input().split())
  bc.append((c,b))
for i in range(N):
  bc.append((A[i], 1))
  
bc=sorted(bc, key=lambda x : x[0], reverse=True)

ans=0
res=N
for x in bc:
  c=x[0]
  b=min(x[1], res)
  ans+=c*b
  res-=b
  if res==0:
    break
    
print(ans)