Buggy python submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun>=s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
c.append(1)
print(c)
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Fixed submission:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
b.sort(key=lambda x:-x[1])
a.sort()
wes=-1
i=0
s=0
res=0
while i<m:
    wes+=b[i][0]
    if wes>n-1:
        res=1
        wes-=b[i][0]
        wes+=1
        break
    if a[wes]>=b[i][1]:
        wes-=b[i][0]
        wes+=1
        break
    else:
        s+=b[i][0]*b[i][1]
        i+=1
if i!=m:
    for j in range(b[i][0]):
        if a[wes+j]<b[i][1]:
            a[wes+j]=b[i][1]
    f=a[wes:]
    s+=sum(f)
else:
    s+=sum(a[wes+1:])
if res=1:
    j=n-1
    while j>=wes:
        if a[j]>b[i][1]:
            s+=a[j]
        else:
            s+=b[i][1]
        j-=1
        
print(s)

Fixed submission:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
b.sort(key=lambda x:-x[1])
a.sort()
wes=-1
i=0
s=0
g=len(b)
while i<g:
    wes+=b[i][0]
    if a[wes]>=b[i][1]:
        wes-=b[i][0]
        wes+=1
        break
    else:
        s+=b[i][0]*b[i][1]
        i+=1
if i!=g:
    for j in range(b[i][0]):
        if a[wes+j]<b[i][1]:
            a[wes+j]=b[i][1]
    f=a[wes:]
    s+=sum(f)
print(s)

Fixed submission:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
b.sort(key=lambda x:-x[1])
a.sort()
wes=-1
i=0
s=0
while True:
    wes+=b[i][0]
    if a[wes]>=b[i][1]:
        wes-=b[i][0]
        wes+=1
        break
    else:
        
        s+=b[i][0]*b[i][1]
        i+=1
for j in range(b[i][0]):
    if a[wes+j]<b[i][1]:
        a[wes+j]=b[i][1]
f=a[wes:]
s+=sum(f)
print(s)

Fixed submission:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))

Buggy python submission (verdict: Runtime Error):
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
b.sort(key=lambda x:-x[1])
a.sort()
print(a)
print(b)
wes=-1
i=0
s=0
while True:
    wes+=b[i][0]
    if a[wes]>=b[i][1]:
        wes-=b[i][0]
        wes+=1
        break
    else:
        
        s+=b[i][0]*b[i][1]
        i+=1
for j in range(b[i][0]):
    if a[wes+j]<b[i][1]:
        a[wes+j]=b[i][1]
f=a[wes:]
print(s)
s+=sum(f)
print(f)
print(s)

Fixed submission:
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
b=[]
s=0
for i in range(m):
    p=list(map(int,input().split()))
    b.append(p)
    s+=p[0]
b.sort(key=lambda x:-x[1])
a.sort()
if s>n:
    s=n
c=[]
i=0
sun=0
while sun<s:
    for j in range(b[i][0]):
        c.append(b[i][1])
    sun+=b[i][0]
    i+=1
for u in range(s):
    if a[u]<c[u]:
        a[u]=c[u]
    
print(sum(a))