Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))

for i in range(m):
    a.sort()
    b,c=map(int,input().split())
    for j in range(b):
        if(a[j]>c):
            break
        a[j]=c

print(sum(a))


Fixed submission:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
        if(k>n-1):
            break
    else:
        continue
    break

print(sum(a))

Buggy python submission (verdict: Runtime Error):
# coding: utf-8
# Your code here!

from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
    else:
        continue
    break

print(sum(a))

Fixed submission:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
        if(k>n-1):
            break
    else:
        continue
    break

print(sum(a))

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    while(bc[i][0]>0):
        j=bc[i][0]
        c=bc[i][1]
        if(j==0):
            break
        if(a[k]>c):
            break
        a[k]=c
        bc[i][0]-=1
        k+=1
    else:
        continue
    break

print(sum(a))

Fixed submission:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
        if(k>n-1):
            break
    else:
        continue
    break

print(sum(a))

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    while(bc[i][0]>0):
        j=bc[i][0]
        c=bc[i][1]
        print([j,c])
        if(j==0):
            break
        if(a[k]>c):
            break
        a[k]=c
        bc[i][0]-=1
        k+=1
    else:
        continue
    break

print(sum(a))

Fixed submission:
from operator import itemgetter

n,m=map(int,input().split())
a=list(map(int,input().split()))
bc=[]

for i in range(m):
    x=list(map(int,input().split()))
    bc.append(x)

a.sort()
bc.sort(key=itemgetter(1),reverse=True)

k=0
for i in range(m):
    b=bc[i][0]
    c=bc[i][1]
    for j in range(b):
        if(a[k]>c):
            break
        a[k]=c
        k+=1
        if(k>n-1):
            break
    else:
        continue
    break

print(sum(a))