Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
BC=[]   #カードの値、枚数
for _ in range(M):
    b,c=map(int, sys.stdin.readline().split())
    BC.append([c,b])

A.sort()
BC.sort(reverse=True)

NEW_A=[]
idx=0   #BCリストのインデックス
for a in A:
    new_value=BC[idx][0]
    if a<new_value:
        NEW_A.append(new_value)
        BC[idx][1]-=1   #個数を-1
    else:
        NEW_A.append(a)
    try:
        if BC[idx][1]==0:
            idx+=1
    except:
        break

print sum(NEW_A)

Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
BC=[]   #カードの値、枚数
for _ in range(M):
    b,c=map(int, sys.stdin.readline().split())
    BC.append([c,b])

A.sort()
BC.sort(reverse=True)

NEW_A=[]
idx=0   #BCリストのインデックス
for a in A:
    new_value=BC[idx][0]
    if a<new_value:
        NEW_A.append(new_value)
        BC[idx][1]-=1   #個数を-1
    else:
        NEW_A.append(a)

    if BC[idx][1]==0:
        idx+=1

print sum(NEW_A)

Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Runtime Error):
import sys
N,M=map(int, sys.stdin.readline().split())
ll=float("-inf")
rr=float("inf")
for _ in range(M):
    l,r=map(int, sys.stdin.readline().split())
    ll=max(ll,l)
    rr=min(rr,r)

if ll<=rr:
    print rr-ll+1
else:
    print 0

Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify
from collections import Counter,defaultdict
dp=defaultdict(lambda: 0)
N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
BC=defaultdict(lambda: 0)
for _ in range(M):
    b,c=map(int, sys.stdin.readline().split())
    BC[c]+=b

C=Counter(A)
A=C.items()
heapify(A)    #(カードの数字、カード枚数)

for c,b in BC.items():
    while A[0][0]<c and 0<b:
        value=A[0][0]
        cards=A[0][1]
        heappop(A)
        if cards<=b:    #取った枚数以上に手元の枚数を持っていれば、カードを全交換できる
            heappush(A,(c,cards))
        else:     #手元の方が少ない場合
            heappush(A,(c,b))   #新しいカードの値を挿入
            heappush(A,(value,cards-b))   #取ったカードも一部元に戻す
        b-=cards

ans=0
for a,cards in A:
    ans+=a*cards
print ans


Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify
from collections import Counter
N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
BC=[map(int, sys.stdin.readline().split()) for _ in range(M)]

C=Counter(A)
A=C.items()
heapify(A)    #(カードの数字、カード枚数)

for b,c in BC:
    while A[0][0]<c and 0<b:
        value=A[0][0]
        cards=A[0][1]
        heappop(A)
        if cards<=b:    #取った枚数以上に手元の枚数を持っていれば、カードを全交換できる
            heappush(A,(c,cards))
        else:     #手元の方が少ない場合
            heappush(A,(c,b))   #新しいカードの値を挿入
            heappush(A,(value,cards-b))   #取ったカードも一部元に戻す
        b-=cards

ans=0
for a,cards in A:
    ans+=a*cards
print ans

Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heappush, heappop, heapify
N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
BC=[map(int, sys.stdin.readline().split()) for _ in range(M)]
heapify(A)

for b,c in BC:
    while A[0]<c and 0<b:
        a=heappop(A)
        heappush(A,c)
        b-=1
print sum(A)

Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heapify, heappush, heappop

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
heapify(A)


for _ in range(M):
	b,c=map(int, sys.stdin.readline().split())
	for i in range(b):
		if A[0]<c:
			heappop(A)
			heappush(A, c)
		else:
			break

print sum(A)


Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from heapq import heapify, heappush, heappop

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
heapify(A)

B=[ map(int, sys.stdin.readline().split()) for i in range(M) ]

for b,c in B:
	for i in range(b):
		if A[0]<c:
			heappop(A)
			heappush(A, c)
		else:
			break

print sum(A)


Fixed submission:
import sys

N,M=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
A.sort()

B=[ map(int, sys.stdin.readline().split()) for _ in range(M)] 
B.sort(key=lambda x: x[1], reverse=True)

i=0

for b,c in B:
	for i in range(i,N):
		if A[i]<c and 0<b:
			A[i]=c
			b-=1
		if b==0:
			break
	i+=1

print sum(A)