Buggy python submission (verdict: Runtime Error):
n,m=map(input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in A]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cut+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))

Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in a]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cnt+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0 for i in range(m)]
c=0
d=0
for i in range(m):
    c,d=map(int,input().split())
    e=[d,c]
    b[i]=e
a.sort()
b.sort(reverse=True)
f=[]
g=n
for i in range(m):
    if(b[i][1]>=g):
        for j in range(g):
            f.append(b[i][0])
            break
    else:
        for k in range(b[i][1]):
            f.append(b[i][0])
        g=g-b[i][1]
if(len(f)<n):
    for i in range(n-len(f)):
        f.append(0)
f.sort(reverse=True)
for i in range(len(f)):
    if(f[i]>=a[i]):
        a[i]=f[i]
    else:
        break
print('{0}'.format(sum(a)))



Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in a]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cnt+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))

Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[0 for i in range(m)]
c=0
d=0
for i in range(m):
    c,d=map(int,input().split())
    e=[d,c]
    b[i]=e
a.sort()
b.sort(reverse=True)
f=[]
g=n
for i in range(m):
    if(b[i][1]>=g):
        for j in range(g):
            f.append(b[i][0])
            break
    else:
        for k in range(b[i][1]):
            f.append(b[i][0])
        g=g-b[i][1]
f.sort(reverse=True)
for i in range(len(f)):
    if(f[i]>=a[i]):
        a[i]=f[i]
    else:
        break
print('{0}'.format(sum(a)))



Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in a]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cnt+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))

Buggy python submission (verdict: Runtime Error):
for i in range(m):
    if(b[i][1]>=g):
        for j in range(g):
            f.append(b[i][0])
            break
    else:
        for j in range(b[i][1]):
            f.append(b[i][0])
        g=g-b[i][1]
if(len(f)<n):
    for i in range(n-len(f)):
        f.append(-1)
f.sort(reverse=True)
for i in range(n):
    if(f[i]>a[i]):
        a[i]=f[i]
    else:
        break
print('{0}'.format(sum(a)))


Fixed submission:
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=[(1,z)for z in a]
b=[tuple(map(int,input().split()))for i in range(m)]
b+=a
b.sort(key=lambda x:x[1])
cnt=0
ans=0
while True:
    c=b.pop()
    if cnt+c[0]<=n:
        ans+=c[0]*c[1]
        cnt+=c[0]
    else:
        ans+=c[1]*(n-cnt)
        break
print('{0}'.format(ans))