Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02580 (python)

Fixed submission:
def main():
    h, w, m = map(int, input().split())
    lsh = [0] * h
    lsw = [0] * w

    bom = set()
    for _ in range(m):
        m1, m2 = map(int, input().split())
        lsh[m1 - 1] += 1
        lsw[m2 - 1] += 1
        bom.add((m1-1, m2-1))

    lshmax = max(lsh)
    lswmax = max(lsw)

    hin = [i for i in range(h) if lsh[i] == lshmax]
    win = [i for i in range(w) if lsw[i] == lswmax]

    for i in hin:
        for j in win:
            if (i, j) not in bom:
                print(lshmax + lswmax)
                exit(0)
    else:
        print(lshmax + lswmax -1)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
h, w, m = map(int, input().split())
lsh = [0] * h
lsw = [0] * w

bom = []
for _ in range(m):
    m1, m2 = map(int, input().split())
    lsh[m1 - 1] += 1
    lsw[m2 - 1] += 1
    bom.append((m1, m2))

lshmax = max(lsh)
lswmax = max(lsw)

for i in range(h)
    if lsh[i] == lshmax:
        for j in range(w):
            if lsw[j] == lswmax:
                if (i,j) in bom:
                    print(lshmax+lswmax-1)
                else:
                    print(lshmax+lswmax)
Problem p02594 (python)

Fixed submission:
x = int(input())
if x >=30:
    print('Yes')
else:
    print('No')

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split())
cn = list(map(int, input().split()))
s = [-1]*(n+1)
t = [0]*n
for i in range(n):
    j = cn[i]
    if s[j] == -1:
        t[i] = -1
        s[j] = i
    else:
        t[i] = s[j]
        s[j] = i

ans = [0]*q
for i in range(q):
    count = 0
    l, r = map(int, input().split())
    for j in range(l-1,r):
        if t[j] >= l-1:
            count += 1
    ans[i] = r-l+1-count

for i in ans:
    print(i)
Problem p02598 (python)

Fixed submission:
n, k = map(int, input().split())
an = list(map(int, input().split()))

maxi = max(an)
if k == 0:
    print(maxi)
    exit(0)


def judge(t):
    m = 0
    for i in an:
        if i%t == 0:
            m +=i//t
        else:
            m += i//t+1
    #print('judge', m, n+k)
    if m <= n+k:
        #print("true")
        return True
    else:
        #print("false")
        return False

mini = 0
#maxi = max(an)

while mini+1 != maxi:
    check = (mini+maxi)//2
    #print(check)
    if judge(check):
        maxi = check
        #print(mini, maxi)
    else:
        mini = check
        #print(mini, maxi)

print(maxi)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
an = list(map(int, input().split()))

def judge(t):
    m = 0
    for i in an:
        m += i//t+1
    if m <= n+k:
        return True
    else:
        return False

mini = 0
maxi = max(an)

while mini != maxi:
    check = int((mini+maxi)/2)
    if judge(check):
        maxi = check
    else:
        mini = check+1

print(mini)
Problem p02627 (python)

Fixed submission:
alpha = str(input())

if alpha in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    print('A')
else:
    print('a')

Buggy submission (verdict: Runtime Error):
import sys
n = int(input())
an = list(map(int, sys.stdin.readline().rstrip().split()))
q = int(input())

import numpy as np

sum = np.sum(an)


for _ in range(q):
    b,c = map(int, input().split())
    for i in range(n):
        if b == an[i]:
            an[i]=c
            sum += c-b
    print(sum)
Problem p02682 (python)

Fixed submission:
a, b, c, k= map(int, input().split())

if k <= a:
    print(k)
elif k <= a +b:
    print(a)

else:
    print(a -(k-a-b))

Buggy submission (verdict: Runtime Error):
if k <= a:
    print(k)
elif k <= a +b:
    print(a)

else:
    print(a -(k-a-b))
Problem p02689 (python)

Fixed submission:
N, M = map(int, input().split())
c_list = [[] for _ in range(N)]
e_list = list(map(int, input().split()))
for _ in range(M):
   a, b = map(lambda i: int(i) - 1, input().split())
   c_list[a].append(b)
   c_list[b].append(a)
ans = 0
for i, e in enumerate(e_list):
   for j in c_list[i]:
       if e <= e_list[j]:
           break
   else:
       ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
from itertools import chain

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

ab = []
for i in range(m):
    ab.append(list(map(int, input().split())))

count = 0

for i in range(1, n+1):

    c= []
    e= []
    for j in ab:
        if i in j:
            c.append(j)
    d =list(chain.from_iterable(c))

    for k in d:
        e.append(H[k-1])

    if len(e)==0:

        continue
    else max(e) == H[i-1]:
        count += 1

print(count)
Problem p02753 (python)

Fixed submission:
s = input()
if s=='AAA' or s== 'BBB':
    print('No')
else:
    print('Yes')

Buggy submission (verdict: Runtime Error):
a, b = map(int, input().split())
x=10*b

import math
for i in range(10):
    if math.floor((x+i)*0.08)==a:
        print(x+i)
        exit(0)

print(-1)
Problem p02759 (python)

Fixed submission:
n = int(input())

print(int(n//2+n%2))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
ans = ['t'] * n

for _ in range(m):
    s, c = map(int, input().split())
    if ans[s-1] =='t' or ans[s-1] == str(c):
        ans[s-1]= str(c)
    else:
        print(-1)
        exit(0)
if ans[0] == 't':
    ans[0]=str(1)

ans = ''.join(ans).replace('t', str(0))
ans = int(ans)
l = str(ans)
if len(l) != n:
    print(-1)
else:
    print(ans)
Problem p02688 (python)

Fixed submission:
n,k = map(int,input().split())
sunuke = []
for _ in range(k):
    d = input()
    tmp = [int(s) for s in input().split()]
    for i in range(len(tmp)):
        sunuke.append(tmp[i])

print(n - len(set(sunuke)))

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
sunuke = []
for _ in range(int(input())):
    tmp = [int(s) for s in input().split()]
    for i in range(len(tmp)):
        sunuke.append(tmp[i])
print(n - len(set(sunuke)))
Problem p03631 (python)

Fixed submission:
array = list(map(str, input()))
if array[0] == array[2]:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
array = list(map(str, input().split()))
if array[0] == array[2]:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
array = list(map(int, input().split()))
if array[0] == array[2]:
    print("Yes")
else:
    print("No")
Problem p02757 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,P=map(int, sys.stdin.readline().split())
S=sys.stdin.readline().strip()
S=S[::-1]

ans=0
if P in (2,5):
    len_S=len(S)
    for i,x in enumerate(S):
        x=int(x)
        if x%P==0:
            ans+=(len_S-i)
    print ans
else:
    L=[0] 
    val=0
    for i,x in enumerate(S):
        x=int(x)
        val+=pow(10,i,P)*x
        L.append(val%P)

    C=defaultdict(lambda: 0)    #mod2019の値の出現個数を記録
    for x in L:
        ans+=C[x]   #自分とmod2019の結果が同じ値となる出現個数をansに足す
        C[x]+=1     #自分のmod2019の値を出現個数に+1カウント
    print ans

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque

S=raw_input()
Q=input()
Query=[ sys.stdin.readline().split() for _ in range(Q) ]

s1=deque()
s2=deque()
judge={}
judge["front"]="s1"
judge["end"]="s2"

cnt=0
for x in Query:
    if x[0]=="1":
        n=x[0]
        cnt+=1
    elif x[0]=="2":
        n,f,c=x[0],x[1],x[2]
    if n=="1":
        judge["front"],judge["end"]=judge["end"],judge["front"]
    elif n=="2":
        if f=="1":
            if judge["front"]=="s1":
                s1.appendleft(c)
            elif judge["front"]=="s2":
                s2.appendleft(c)
        elif f=="2":
            if judge["end"]=="s1":
                s1.append(c)
            if judge["end"]=="s2":
                s2.append(c)

str1=""
for x in s1:
    str1+=x
str2=""
for x in s2:
    str2+=x

if cnt%2==0:
    print str1[::-1]+S+str2
elif cnt%2==1:
    print str2+S[::-1]+str1[::-1]
Problem p02862 (python)

Fixed submission:
# -*- coding: utf-8 -*-

X,Y=map(int, raw_input().split())

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, max(X,Y)+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

if (X+Y)%3!=0:
    print 0
else:
    m=(2*X-Y)/3
    n=(Y-m)/2
    print ncr(m+n,n)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

X,Y=map(int, raw_input().split())

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, max(X,Y)+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

if X==Y:
    if X%3!=0:
        print 0
    else:
        x=X/3
        print ncr(x*2,x)
else:
    if X<Y: X,Y=Y,x
    sho=X/2
    x=X-(sho*2)
    y=Y-sho
    if (x,y)==(0,0):
        print 1
    elif (x,y)!=(1,2):
        print 0
    else:
        print ncr(sho+1,sho)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

X,Y=map(int, raw_input().split())

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, max(X,Y)+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

if X==Y:
    if X%3!=0:
        print 0
    else:
        x=X/3
        print ncr(x*2,x)
else:
    if X<Y: X,Y=Y,x
    sho=X/2
    x=X-(sho*2)
    y=Y-sho
    if (x,y)==(0,0):
        print 0
    elif (x,y)!=(1,2):
        print 0
    else:
        print ncr(sho+1,sho)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

X,Y=map(int, raw_input().split())

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, max(X,Y)+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

if X==Y:
    if X%3!=0:
        print 0
    else:
        x=X/3
        print ncr(x*2,x)
else:
    if X<Y: X,Y=Y,x
    sho=X/2
    x=X-(sho*2)
    y=Y-sho
    if (x,y)!=(1,2):
        print 0
    else:
        print ncr(sho+1,sho)
Problem p02863 (python)

Fixed submission:
# -*- coding: utf-8 -*-

import sys
N,T=map(int, sys.stdin.readline().split())
AB=[ map(int, sys.stdin.readline().split()) for _ in range(N) ]

AB.sort()

N=6005
dp=[ None for _ in range(N) ]
dp[0]=0

for a,b in AB:
    for i in range(N-1,-1,-1):
        if dp[i] is not None and i<T:
                dp[i+a]=max(dp[i+a],dp[i]+b)

print max(dp)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

import sys
N,T=map(int, sys.stdin.readline().split())
AB=[ map(int, sys.stdin.readline().split()) for _ in range(N) ]

AB.sort()

N=2005
dp=[ None for _ in range(N) ]
dp[0]=0

for a,b in AB:
    for i in range(N-1,-1,-1):
        if dp[i] is not None and i<T:
                dp[i+a]=max(dp[i+a],dp[i]+b)

print max(dp)
Problem p02884 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import deque

N,M=map(int, sys.stdin.readline().split())
al=[ [] for i in range(N+1) ]	#隣接リスト
rev_al=[ [] for i in range(N+1) ]	#リバース隣接リスト
O=[ 0 for i in range(N+1) ] #出次数
edge=[] #辺の集合

for _ in range(M):
    s,t=map(int, sys.stdin.readline().split())
    al[s].append(t)
    rev_al[t].append(s)
    #I[t]+=1
    O[s]+=1
    edge.append((s,t))


#頂点毎の到達する確立
P=[ 0 for i in range(N+1) ]   #確率
P[1]=1
 
for x in range(1,N+1):
    if O[x]!=0:
        p=float(1)/O[x]
        for to in al[x]:
            P[to]+=P[x]*p
        
#頂点毎の期待値
E=[ 0 for i in range(N+1) ]   #期待値

for x in range(N,0,-1):
    for to in rev_al[x]:
        p=float(1)/O[to]
        E[to]+=(E[x]+1)*p


#消す辺
ans=E[1]
for del_s,del_t in edge:
    if O[del_s]-1==0:
        continue
    new_E=float(E[del_s]*O[del_s]-(E[del_t]+1))/(O[del_s]-1)
    #print "del_s,del_t,new_E, :: ",del_s,del_t,new_E,E,E[1]+(new_E-E[del_s])*P[del_s]
    if new_E<E[del_s]:
        ans=min(ans,E[1]-(E[del_s]-new_E)*P[del_s])

print ans

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from collections import deque

N,M=map(int, sys.stdin.readline().split())
al=[ [] for i in range(N+1) ]	#隣接リスト
rev_al=[ [] for i in range(N+1) ]	#リバース隣接リスト
I=[ 0 for i in range(N+1) ] #入次数
O=[ 0 for i in range(N+1) ] #出次数
edge=[] #辺の集合
I[0]=None

for _ in range(M):
    s,t=map(int, sys.stdin.readline().split())
    al[s].append(t)
    rev_al[t].append(s)
    I[t]+=1
    O[s]+=1
    edge.append((s,t))

#トポロジカルソート
L=[]
q=deque()
q.append(1) #startは常に1

while q:
    fro=q.popleft()
    L.append(fro)
    for to in al[fro]:
        I[to]-=1
        if I[to]==0:
            q.append(to)

if len(L)!=N:
    prin

#頂点毎の到達する確立
P=[ 0 for i in range(N+1) ]   #確率
P[1]=1

for x in L:
    if O[x]!=0:
        p=float(1)/O[x]
        for to in al[x]:
            P[to]+=P[x]*p
        
#頂点毎の期待値
E=[ 0 for i in range(N+1) ]   #期待値

for x in L[::-1]:
    for to in rev_al[x]:
        p=float(1)/O[to]
        E[to]+=(E[x]+1)*p
#消す辺
ans=E[1]
for del_s,del_t in edge:
    if O[del_s]-1==0:
        continue
    new_E=float(E[del_s]*O[del_s]-(E[del_t]+1))/(O[del_s]-1)
    #print "del_s,del_t,new_E, :: ",del_s,del_t,new_E,E,E[1]+(new_E-E[del_s])*P[del_s]
    if new_E<E[del_s]:
        ans=min(ans,E[1]-(E[del_s]-new_E)*P[del_s])

print ans
Problem p02955 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left, bisect_right

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

s=sum(A)
n=int(s**0.5)

#約数のリスト
cd=[]

for i in range(1,n+2):
	if s%i==0:
		cd.append(i)
		cd.append(s/i)

cd.sort(reverse=True)

def check(t):
	if t==1: return True
	A_temp=[]
	for x in A:
		A_temp.append(x%t)

	A_temp.sort()

	#Aの配列をmod tした配列A_tempの累積和
	A_temp_sum=[]

	for i,x in enumerate(A_temp):
		if i==0: A_temp_sum.append(x)
		else: A_temp_sum.append(A_temp_sum[-1]+x)

	#配列の前半は、数字をマイナスしていく
	p1=min(bisect_left(A_temp_sum,K),N-2)
	if A_temp_sum[p1]<=K:
		k1=A_temp_sum[p1]
	else:
		#配列の項数p1の値がKを超えている場合は、p1の1個手前の値をとる
		p1-=1
		k1=A_temp_sum[p1]


	#配列の後半で、数字をどれだけプラスする必要があるか
	k2=t*(N-p1-1)-(A_temp_sum[-1]-A_temp_sum[p1])

	#プラスする量が、マイナスする量より少なければ成立
	if k2<=k1:
		return True
	else:
		return False

for x in cd:
	if check(x):
		print x
		quit()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from bisect import bisect_left, bisect_right

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

s=sum(A)
n=int(s**0.5)

#約数のリスト
cd=[]

for i in range(1,n+2):
	if s%i==0:
		cd.append(i)
		cd.append(s/i)

cd.sort(reverse=True)

def check(t):
	A_temp=[]
	for x in A:
		A_temp.append(x%t)

	A_temp.sort()

	#Aの配列をmod tした配列A_tempの累積和
	A_temp_sum=[]

	for i,x in enumerate(A_temp):
		if i==0: A_temp_sum.append(x)
		else: A_temp_sum.append(A_temp_sum[-1]+x)

	#配列の前半は、数字をマイナスしていく
	p1=bisect_left(A_temp_sum,K)
	if A_temp_sum[p1]<=K:
		k1=A_temp_sum[p1]
	else:
		p1-=1
		k1=A_temp_sum[p1]

	#配列の後半で、数字をどれだけプラスする必要があるか
	k2=t*(N-p1-1)-(A_temp_sum[-1]-A_temp_sum[p1])

	#プラスする量が、マイナスする量より少なければ成立
	if k2<=k1:
		return True
	else:
		return False

for x in cd:
	if check(x):
		print x
		quit()
Problem p02984 (python)

Fixed submission:
import sys
from collections import deque

N=input()
A=map(int, sys.stdin.readline().split())
A=map(lambda x: x*2, A)

a=sum(A)/2

for i in range(1,N):
	if i%2==1:
		a-=A[i]

#a
print a,

prev=a
for x in A[:-1]:
	x-=prev
	print x,
	prev=x

Buggy submission (verdict: Runtime Error):
from numpy import array
from numpy.linalg import solve

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

L=[]
for i in range(N-1):
	L.append ([0]*i+[1,1]+[0]*(N-i-2)) 

L.append([1]+[0]*(N-2)+[1])


a=array(L) 
b=array(A)       
L=solve(a,b) 

for ans in L:
	print (int(ans)*2, end=" ")

Buggy submission (verdict: Runtime Error):
from numpy import array
from numpy.linalg import solve
import sys

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

L=[]
for i in range(N-1):
	L.append ([0]*i+[1,1]+[0]*(N-i-2)) 

L.append([1]+[0]*(N-2)+[1])


a=array(L) 
b=array(A)       
L=solve(a,b) 

for ans in L:
	print int(ans)*2,
Problem p03017 (python)

Fixed submission:
N,A,B,C,D=map(int, raw_input().split())
S=raw_input()

if C<D:
	if "##"  in S[B-1:D]:
		print "No"
		quit()
	if "##" in S[A-1:C]:
		print "No"
		quit()
	print "Yes"
elif D<C:
	if "##" in S[A-1:C]:
		print "No"
		quit()

	if "..." in S[B-2:D+1]:
		print "Yes"
		quit()
	print "No"

Buggy submission (verdict: Runtime Error):
N,A,B,C,D=map(int, raw_input().split())
S=raw_input()

if C<D:
	if "##"  in S[B-1:D]:
		print "No"
		quit()
	if "##" in S[A-1:C]:
		print "No"
		quit()
	print "Yes"
elif D<C:
	if "##" in S[A-1:C]:
		print "No"
		quit()

	for i in range(B-1,D):
		if S[i]==S[i+1]==S[i+2]==".":
			print "Yes"
			quit()

	print "No"
Problem p03033 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right

N,Q=map(int, sys.stdin.readline().split())

S=[]
event=[]	#x, s, t
for _ in range(N):
	s,t,x=map(int, sys.stdin.readline().split())
	heappush( event, (x, max(0,s-x) , max(0,t-1-x)) )


D=[ int(sys.stdin.readline().strip()) for _ in range(Q) ]
D.sort()
ans=[ float("inf") for _ in range(Q) ]

Next=[ i+1 for i in range(Q) ]	#Next


while event:
	x,s,t=heappop(event)
	start=bisect_left(D,s)
	end=bisect_right(D,t)

	i=start
	while i<end:
		ans[i]=min(ans[i],x)
		next=Next[i]
		Next[i]=end
		i=next

for x in ans:
	if x==float("inf"):
		print -1
	else:
		print x

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys
from heapq import heappush, heappop, heapify
from collections import defaultdict
import copy
 
N,Q=map(int, sys.stdin.readline().split())
 
S=[]
event=[]
for _ in range(N):
	s,t,x=map(int, sys.stdin.readline().split())
	event.append(  ( s-x ,1, x) )	#Start
	event.append(  ( t-x,-1, x) )	#End


for i in range(Q):
	d=int(sys.stdin.readline().strip())
	event.append(  (d ,0, i) )

event.sort(key=lambda x:(x[0]) )

stop=set()

ans=defaultdict(lambda: -1) 

for s,flag,x in event:
	if flag==1:
		stop.add(x)
	elif flag==-1:
		stop.remove(x)
	elif flag==0:
		if stop:
			ans[x]=min(stop)
		else:
			ans[x]=-1


for i in range(Q):
	print ans[i]
Problem p03038 (python)

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 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)

Buggy 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)

Buggy 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
Problem p03045 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

N,M=map(int, raw_input().split())
par=[ i for i in range(N+1)  ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y


for i in range(M):
	x,y,z=map(int, raw_input().split())
	unite(x,y)


S=set()
for i in range(1,N+1):
	S.add(root(i))

print len(S)

Buggy submission (verdict: Runtime Error):
N,M=map(int, raw_input().split())

par=[ i for i in range(N+1)  ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y



for i in range(M):
	x,y,z=map(int, raw_input().split())
	if not same(x,y):
		unite(x,y)


S=set()
for i in range(1,N+1):
	S.add(root(i))

print len(S)

Buggy submission (verdict: Runtime Error):
N=input()

nl=[ [] for i in range(N+1) ]

q=deque()
for i in range(N-1):
	u,v,w=map(int, raw_input().split())
	nl[u].append( (v,w) )
	nl[v].append( (u,w) )

	if i==0:
		Visit[u]=0
		q.append(1)

Visit=[ None for i in range(N+1) ] 


while q:
	x=q.popleft()
	for to,kyori in nl[x]:
		if Visit[to] is None:
			if kyori%2==0:
				Visit[to]=Visit[x]
			else:
				Visit[to]=(Visit[x]+1)%2
			q.append(to)
			

for i in Visit[1:]:
	print i

Buggy submission (verdict: Runtime Error):
from collections import deque

N=input()

nl=[ [] for i in range(N+1) ]

q=deque()
for i in range(N-1):
	u,v,w=map(int, raw_input().split())
	nl[u].append( (v,w) )
	nl[v].append( (u,w) )

	if i==0:
		Visit[u]=0
		q.append(1)

Visit=[ None for i in range(N+1) ] 


while q:
	x=q.popleft()
	for to,kyori in nl[x]:
		if Visit[to] is None:
			if kyori%2==0:
				Visit[to]=Visit[x]
			else:
				Visit[to]=(Visit[x]+1)%2
			q.append(to)
			

for i in Visit[1:]:
	print i

Buggy submission (verdict: Runtime Error):
from collections import deque

N,M=map(int, raw_input().split())


par=[ i for i in range(N+1)  ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y



for i in range(M):
	x,y,z=map(int, raw_input().split())
	unite(x,y)


S=set()
for i in range(1,N+1):
	S.add(root(i))


print len(S)
Problem p03108 (python)

Fixed submission:
import sys
from collections import defaultdict
sys.setrecursionlimit(1000000)

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

par=[ i for i in range(N+1) ]

size=[ 1 for i in range(N+1) ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y
	size[y]+=size[x]

def getsize(x):
	return size[root(x)]


ans=N*(N-1)/2

L=[]

for a,b in reversed(A):
	L.append(max(0,ans))
	if root(a)==root(b):
		n1=getsize(a)
		ans+=n1*(n1-1)/2	
	else:
		n1=getsize(a)
		n2=getsize(b)
		ans+=n1*(n1-1)/2
		ans+=n2*(n2-1)/2

	unite(a,b)

	n=getsize(a)
	ans-=n*(n-1)/2



for x in reversed(L):
	print x

Buggy submission (verdict: Runtime Error):
import sys
from collections import defaultdict

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

par=[ i for i in range(N+1) ]

size=[ 1 for i in range(N+1) ]

def root(x):
	if par[x]==x:
		return x
	else:
		par[x]=root( par[x] )
		return par[x]

def same(x,y):
	return root(x)==root(y)

def unite(x,y):
	x=root(x)
	y=root(y)
	if x==y: return
	par[x]=y
	size[y]+=size[x]

def getsize(x):
	return size[root(x)]


ans=N*(N-1)/2

L=[]

for a,b in reversed(A):
	L.append(max(0,ans))
	if root(a)==root(b):
		n1=getsize(a)
		ans+=n1*(n1-1)/2	
	else:
		n1=getsize(a)
		n2=getsize(b)
		ans+=n1*(n1-1)/2
		ans+=n2*(n2-1)/2

	unite(a,b)

	n=getsize(a)
	ans-=n*(n-1)/2



for x in reversed(L):
	print x