Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def f(mtrx):
	maxSum = -10**9
	for rowStart in range(n):
		for  rowEnd in range(rowStart,n):
			for colStart in range(n):
				for colEnd in range(colStart,n):
					calcSum = 0
					for row in range(rowStart,rowEnd+1):
						calcSum += sum(mtrx[row][colStart:colEnd+1])
					maxSum = max(calcSum, maxSum)
	return maxSum
	
n = int(raw_input())
mtrx = [map(int, raw_input().split()) for i in range(n)]
print f(mtrx)

Fixed submission:
def x(a):
	m=-10**9
	for c in N:
		p=[0]*n 
		for e in range(c,n):
			for r in N:
				p[r]+=a[r][e]
			m=max(P(p),m)
	return m
def P(a):
	m,c=-10**5,0
	for i in N:
		c+=a[i]
		m=max(c,m)
		if c<0:c=0
	return m
n = input()
N = range(n)
print x([map(int, raw_input().split()) for i in N])

Buggy python submission (verdict: Time Limit Exceeded):
def f(mtrx):
	maxSum = -10000000
	partSum = [0]*n
	for colStart in range(n):
		for colEnd in range(colStart,n):
			for row in range(n):
				partSum[row] = sum(mtrx[row][colStart:colEnd+1])
			calcSum = maxPart(partSum)
			maxSum = max(calcSum, maxSum)
	return maxSum

def maxPart(mtrx):
	maxSum = -10000000
	for startP in range(n):
		for endP in range(startP,n):
			calcSum = sum(mtrx[startP:endP+1])
			maxSum = max(calcSum,maxSum)
	return maxSum
	
n = int(raw_input())
mtrx = [map(int, raw_input().split()) for i in range(n)]
print f(mtrx)

Fixed submission:
def x(a):
	m=-10**9
	for c in N:
		p=[0]*n 
		for e in range(c,n):
			for r in N:
				p[r]+=a[r][e]
			m=max(P(p),m)
	return m
def P(a):
	m,c=-10**5,0
	for i in N:
		c+=a[i]
		m=max(c,m)
		if c<0:c=0
	return m
n = input()
N = range(n)
print x([map(int, raw_input().split()) for i in N])

Buggy python submission (verdict: Time Limit Exceeded):
def f(mtrx):
	maxSum = -10000000
	for colStart in range(n):
		partSum = [0]*n
		for colEnd in range(colStart,n):
			for row in range(n):
				partSum[row] += mtrx[row][colEnd]
			calcSum = maxPart(partSum)
			maxSum = max(calcSum, maxSum)
	return maxSum

def maxPart(mtrx):
	maxSum = -10000000
	for startP in range(n):
		for endP in range(startP,n):
			calcSum = sum(mtrx[startP:endP+1])
			maxSum = max(calcSum,maxSum)
	return maxSum
	
n = int(raw_input())
mtrx = [map(int, raw_input().split()) for i in range(n)]
print f(mtrx)

Fixed submission:
def x(a):
	m=-10**9
	for c in N:
		p=[0]*n 
		for e in range(c,n):
			for r in N:
				p[r]+=a[r][e]
			m=max(P(p),m)
	return m
def P(a):
	m,c=-10**5,0
	for i in N:
		c+=a[i]
		m=max(c,m)
		if c<0:c=0
	return m
n = input()
N = range(n)
print x([map(int, raw_input().split()) for i in N])
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, raw_input().split())
s = [0 for i in range(n)]
for i in range(q):
	a, v = map(int, raw_input().split())
	s[a-1] += v
	print s.index(max(s))+1, max(s)

Fixed submission:
n, q = map(int, raw_input().split())
s = [0]*n
mx,mxa = 0, 0
for i in range(q):
	a,v = map(int, raw_input().split())
	s[a-1] += v
	if v > 0:
		if s[a-1] > mx:
			mx = s[a-1]
			mxa = a-1
		elif s[a-1] == mx:
			if a-1 < mxa:
				mxa = a-1
	elif v < 0:
		if a-1 == mxa:
			mx = max(s)
			mxa = s.index(mx)
	print mxa+1,mx

Buggy python submission (verdict: Runtime Error):
n, q = map(int, raw_input().split())
s = [0 for i in range(n)]
a = [map(int, raw_input().split()) for i in range(q)]
for r in a:
	s[r[0]] += r[1]
	mx = max(s)
	print s.index(mx)+1, mx

Fixed submission:
n, q = map(int, raw_input().split())
s = [0]*n
mx,mxa = 0, 0
for i in range(q):
	a,v = map(int, raw_input().split())
	s[a-1] += v
	if v > 0:
		if s[a-1] > mx:
			mx = s[a-1]
			mxa = a-1
		elif s[a-1] == mx:
			if a-1 < mxa:
				mxa = a-1
	elif v < 0:
		if a-1 == mxa:
			mx = max(s)
			mxa = s.index(mx)
	print mxa+1,mx

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, raw_input().split())
s = [0 for i in range(n)]
a = [map(int, raw_input().split()) for i in range(q)]
for r in a:
	s[r[0]-1] += r[1]
	mx = max(s)
	print s.index(mx)+1, mx

Fixed submission:
n, q = map(int, raw_input().split())
s = [0]*n
mx,mxa = 0, 0
for i in range(q):
	a,v = map(int, raw_input().split())
	s[a-1] += v
	if v > 0:
		if s[a-1] > mx:
			mx = s[a-1]
			mxa = a-1
		elif s[a-1] == mx:
			if a-1 < mxa:
				mxa = a-1
	elif v < 0:
		if a-1 == mxa:
			mx = max(s)
			mxa = s.index(mx)
	print mxa+1,mx

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, raw_input().split())
s = [0]*n
for i in range(q):
	a,v = map(int, raw_input().split())
	s[a-1] += v
	ss = sorted(s,reverse=True)
	print s.index(ss[0])+1,ss[0]

Fixed submission:
n, q = map(int, raw_input().split())
s = [0]*n
mx,mxa = 0, 0
for i in range(q):
	a,v = map(int, raw_input().split())
	s[a-1] += v
	if v > 0:
		if s[a-1] > mx:
			mx = s[a-1]
			mxa = a-1
		elif s[a-1] == mx:
			if a-1 < mxa:
				mxa = a-1
	elif v < 0:
		if a-1 == mxa:
			mx = max(s)
			mxa = s.index(mx)
	print mxa+1,mx
Buggy python submission (verdict: Runtime Error):
n = int(raw_input())
for i in range(n):
    inp = map(str, raw_input().split())
    for j in range(len(inp)):
        if inp[j][:7] == "Hoshino":
           inp[j] = Hoshina" + inp[j][7:]
    print " ".join(map(str, inp))
    

Fixed submission:
for i in [0]*input():print raw_input().replace("Hoshino","Hoshina")
Buggy python submission (verdict: Runtime Error):
while True:
    w, h = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    print tile
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y   ; break
        else:        print "LOOP"; break

Fixed submission:
while True:
    h, w = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y; break
        else: print "LOOP"; break

Buggy python submission (verdict: Runtime Error):
while True:
    w, h = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    print tile
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y   ; break
        else:            print "LOOP"; break

Fixed submission:
while True:
    h, w = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y; break
        else: print "LOOP"; break

Buggy python submission (verdict: Runtime Error):
while True:
    w, h = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    print tile
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y; break
        else: print "LOOP"; break

Fixed submission:
while True:
    h, w = map(int, raw_input().split())
    if w == 0 and h == 0:
        break
    tile = []
    for i in range(h):
        tile.append(list(raw_input()))
    x = y = 0
    while True:
        a = tile[y][x]
        tile[y][x] = 0
        if   a == ">": x += 1
        elif a == "<": x -= 1
        elif a == "^": y -= 1
        elif a == "v": y += 1
        elif a == ".": print x,y; break
        else: print "LOOP"; break
Buggy python submission (verdict: Runtime Error):
index = {}
while True:
    try:
        w, p = map(str, raw_input().split())
        index.setdefault(w,[]).append(int(p))
index.sort()
for i in index:
    index[i].sort()
    print i
    print " ".join(map(int, index[i]))

Fixed submission:
index = {}
while True:
    try:
        w, p = map(str, raw_input().split())
        index.setdefault(w,[]).append(int(p))
    except:
        break

for k,v in sorted(index.items()):
   print k
   print " ".join(map(str, sorted(v)))
Buggy python submission (verdict: Runtime Error):
while 1:
	n = input()
	if n == 0: break
	q = sorted([input() afor i in range(n)])
	t = [0]*n
	for i in range(n-1):
		t[i + 1] = t[i] + q[i]
	print sum(t)

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	q = sorted([input() for i in range(n)])
	t = [0]*n
	for i in range(n-1):
		t[i + 1] = t[i] + q[i]
	print sum(t)
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	A = [raw_input() for i in range(H)]
	S = [[0]*W for i in range(H)]
	L = ans = 0
	for h in range(H):
		for w in range(W):
			if A[h][w] == ".":
				L += 1
			else:
				L = 0
			S[h][w] = L
			if L > 0:
				a = L
				for hi in range(h-1,-1,-1):
					if S[hi][w] == 0:
						break
					a = min(a,S[hi][w])
					ans = max(ans,a*(h-hi+1))
	print ans

Fixed submission:
while 1:
	H,W = map(int,raw_input().split())
	if H == 0: break
	M = [raw_input() for i in range(H)]
	A = [[0]*W for i in range(H)]
	B = [[0]*W for i in range(H)]
	ans = 0
	for h in range(H):
		L = 0
		for w in range(W):
			if M[h][w] == ".":
				L += 1
			else:
				L = 0
			A[h][w] = L
			B[h][w] = (B[h-1][w] if h > 0 else 0) + 1 if L > 0 else 0
			if L*B[h][w] > ans:
				a = L
				for hi in range(B[h][w]):
					a = min(a,A[h-hi][w])
					if a*B[h][w] <= ans:
						break
					ans = max(ans,a*(hi+1))
	print ans
Buggy python submission (verdict: Runtime Error):
n=input()
while 1:
	m=[raw_input().split() for i in range(n)]
	p={}
	for t in m:
		p[t[0]] = 3*int(t[1])+1*int(t[3])
	for k,v in sorted(p.items(),key=lambda x:x[1])[::-1]:
		print "%s,%d"%(k,v)
	c+=1
	n=input()
	if n==0:break
	print

Fixed submission:
n=input()
while 1:
	p={}
	for i in range(n):
		t=raw_input().split()
		p[t[0]] = [3*int(t[1])+1*int(t[3]),-i]
	for k,v in sorted(p.items(),key=lambda x:x[1])[::-1]:
		print "%s,%d"%(k,v[0])
	n=input()
	if n==0:break
	print
Buggy python submission (verdict: Runtime Error):
histo = ["" for i in range(6)]
n = int(raw_input())
for i in range(n):
    h = float(raw_input())
    if h < 165.0:
        histo[0] += "*"
    elif h < 170.0:
        histo[1] += "*"
    elif h < 175.0:
        histo[2] += "*"
    elif h < 180.0:
        histo[3] += "*"
    elif h < 185.0:
        histo[4] += "*"
    else:
        histo[5] += "*"

for i in range(6):
    print "%d:%a"%(i,a[i])

Fixed submission:
f = ["" for i in range(6)]
n = int(raw_input())
for i in range(n):
    h = float(raw_input())
    if   h < 165.0: f[0] += "*"
    elif h < 170.0: f[1] += "*"
    elif h < 175.0: f[2] += "*"
    elif h < 180.0: f[3] += "*"
    elif h < 185.0: f[4] += "*"
    else          : f[5] += "*"

for i in range(6):
    print "%d:%-s"%(i+1,f[i])
Buggy python submission (verdict: Runtime Error):
n = int(raw_input())
for i in range(n):
    s = int(raw_input())
    print "Case %d:"%(i+1)
    for j in range(10):
        s =int(str(s**2)[-6:-2])
        print s

Fixed submission:
n = int(raw_input())
for i in range(n):
    s = int(raw_input())
    print "Case %d:"%(i+1)
    for j in range(10):
        if len(str(s**2)) < 3:
            s = 0
        else:
            s =int(str(s**2)[-6:-2])
        print s
Buggy python submission (verdict: Runtime Error):
stop=[0,1,2,3,4,5,6,7,8,9,5,4,3,2,1,0]
for i in range(input()):
	sp,ep=map(int,raw_input().split())
	if sp<ep:
		print stop[stop.index(sp):stop.index(ep)+1]
	else:
		print stop[stop.rindex(sp):stop.rindex(ep)+1]

Fixed submission:
p="0123456789543210"
for i in range(input()):
	sp,ep=raw_input().split()
	(sp,ep)=(p.find(sp),p.find(ep)+1) if sp<ep else (p.rfind(sp),p.rfind(ep)+1)
	print " ".join(p[sp:ep]) if sp<ep else " ".join(p[sp:]+p[1:ep])
Buggy python submission (verdict: Runtime Error):
for r in range(input()):
	if r > 0: print
	n = input()
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if y+dy == -1: raise
			if S[y+dy][x+dx] == "#":
				y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])

Fixed submission:
for r in range(input()):
	if r > 0: print
	n = input()
	if n < 3:
		for i in range(n): print "#"*(n-i) + " "*i
		continue
	S = [[" "]*n for i in range(n)]
	x,y,dx,dy = 0,n-1,0,-1
	while 1:
		S[y][x] = "#"
		y += dy; x += dx
		try:
			if S[y+dy][x+dx] == "#":
				if y+dy != -1:
					y -= dy; x -= dx
				raise
		except:
			S[y][x] = "#"
			dx,dy = -dy,dx
			y += dy; x += dx
		if S[y+dy][x+dx] == "#" or S[y+dx][x-dy] == "#": break
	for i in range(n):
		print "".join(S[i])
Buggy python submission (verdict: Runtime Error):
ref = [1.1,0.6,0.2,0.0]
rank = [[0,0] for i in range(4)]
while True:
    try:
        eye = map(float, raw_input().split())
        for i in range(2): 
            for j in range(4):
                if eye[i] >= ref[j]: rank[j][i] += 1
                break
    except:
        break

    print " ".join(map(str, rank[i])) for i in range(4)

Fixed submission:
from bisect import bisect
ref = [0.2,0.6,1.1]
l,r = [0]*4,[0]*4
while 1:
    try:
        le,re = map(float, raw_input().split())
    except:
        break
    l[bisect(ref,le)] += 1
    r[bisect(ref,re)] += 1
for i in range(1,5):
	print l[-i],r[-i]
Buggy python submission (verdict: Time Limit Exceeded):
def up(n):
	if n == 0: return 1
	if n < 0: return 0
	return up(n-1)+up(n-2)+up(n-3)
	
while True:
	n = int(raw_input())
	if n == 0: break
	print (up(n)//10+1)//365+1

Fixed submission:
def up(n):
	goal = [0 for i in range(n)]
	goal[:3] = [1,2,4]
	for i in range(3,n):
		goal[i] = sum(goal[i-3:i])
	return goal[-1]
	
while True:
	n = int(raw_input())
	if n == 0: break
	print (up(n)//10+1)//365+1
Buggy python submission (verdict: Runtime Error):
import itertools

def isOK(array):
	for i in range(n-1):
		if int(array[i][2])<sum([int(array[i][1]) for x in range(i+1,n)]):
			return False
	return G(array)

def G(array):
	nume=sum([(i+1)*int(array[i][1]) for i in range(n)])*1.0
	denomi=sum([int(array[i][1]) for i in range(n)])
	return nume/denomi
	
while 1:
	n=input()
	if n==0:break
	lunch=[raw_input().split() for i in range(n)]
	ans,minG=[],10000
	for order in itertools.permutations(lunch,n):
		print order
		val=isOK(order)
		if val is not False and val<minG:
			ans=[order[i][:] for i in range(n)]
			minG=val
	for i in range(n):
		print ans[i][0]

Fixed submission:
def stuck(array):
	order=[]
	while array:
		r = len(array)
		w = sum([array[i][1] for i in range(r)])
		mx = 0
		for i in range(r):
			if array[i][2] >= w-array[i][1] and array[i][1] > mx:
				mx = array[i][1]
				id = i
		order.append(array.pop(id)[0])
	return order

def change(a):
	return a[0],int(a[1]),int(a[2])
	
while 1:
	n = input()
	if n == 0:break
	lunch = [change(raw_input().split()) for i in range(n)]
	ans = stuck(lunch)
	for i in range(n):
		print ans[i]

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
from copy import deepcopy

def stuck(Array,Order):
	remain = len(Array)
	if remain == 0: return G(Order),Order
	minG,minOrder = 9999999,[]
	w = sum([int(Array[i][1]) for i in range(remain)])
	for i in range(remain):
		if int(Array[i][2]) >= w-int(Array[i][1]):
			array = deepcopy(Array)
			order = deepcopy(Order)
			order[n-remain] = array.pop(i)
			curG,curOrder = stuck(array,order)
			if curG < minG:
				minG = curG
				minOrder = curOrder
	return minG,minOrder

def G(array):
	nume=sum([(i+1)*int(array[i][1]) for i in range(n)])*1.0
	denomi=sum([int(array[i][1]) for i in range(n)])
	return nume/denomi
	
while 1:
	n=input()
	if n==0:break
	lunch=[raw_input().split() for i in range(n)]
	minG,ans=stuck(lunch,[0]*n)
	for i in range(n):
		print ans[i][0]

Fixed submission:
def stuck(array):
	order=[]
	while array:
		r = len(array)
		w = sum([array[i][1] for i in range(r)])
		mx = 0
		for i in range(r):
			if array[i][2] >= w-array[i][1] and array[i][1] > mx:
				mx = array[i][1]
				id = i
		order.append(array.pop(id)[0])
	return order

def change(a):
	return a[0],int(a[1]),int(a[2])
	
while 1:
	n = input()
	if n == 0:break
	lunch = [change(raw_input().split()) for i in range(n)]
	ans = stuck(lunch)
	for i in range(n):
		print ans[i]
Buggy python submission (verdict: Runtime Error):
for i in range(9):
    a,b,c = map(int, raw_input().split())
    print a,b+c,200*b+300*c

Fixed submission:
for i in range(9):
    a,b,c = map(str, raw_input().split())
    b,c = int(b),int(c)
    print a,b+c,200*b+300*c
Buggy python submission (verdict: Runtime Error):
 def f(n):
	a = ""
	while n>0:
		a += str(n%4)
		n //= 4
	return a[::-1]
	
while 1:
	n = input()
	if n == -1: break
	print f(n) if n > 0 else 0

Fixed submission:
def f(n):
	a = ""
	while n>0:
		a += str(n%4)
		n //= 4
	return a[::-1]
	
while 1:
	n = input()
	if n == -1: break
	print f(n) if n > 0 else 0
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	m,n = map(int,raw_input().split())
	if m==0: break
	book = [int(raw_input()) for i in range(n)]
	
	for i in range(n):
		max_shelf = sum(book[:i+1])
		cur_shelf,num = 0,2
		j = i+1
		while j < n:
			if num > m: break
			if cur_shelf + book[j] > max_shelf:
				num += 1
				cur_shelf = 0
			else:
				cur_shelf += book[j]
				j += 1
		else:
			break
	
	flag = 0
	unit = min(book)
	while 1:
		max_shelf -= unit
		cur_shelf,num = 0,2
		j = i+1
		while j < n:
			if num > m:
				flag = 1
				break
			if cur_shelf + book[j] > max_shelf:
				num += 1
				cur_shelf = 0
			else:
				cur_shelf += book[j]
				j += 1
		if flag == 1: break
	print max_shelf+unit

Fixed submission:
def isStorage(max_shelf):
	i,cur_shelf,num = 0,0,1
	while i < n:
		if num > m:
			return False
		if cur_shelf + book[i] > max_shelf:
			num += 1
			cur_shelf = 0
		else:
			cur_shelf += book[i]
			i += 1
	else:
		return True
			
while 1:
	m,n = map(int,raw_input().split())
	if m==0: break
	book = [int(raw_input()) for i in range(n)]
	max_shelf = len = sum(book)/2
	while len:
		len /= 2
		max_shelf -= len if isStorage(max_shelf) else -len
	max_shelf += 6
	while isStorage(max_shelf-1):
		max_shelf -= 1
	print max_shelf

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	m,n = map(int,raw_input().split())
	if m==0: break
	book = [int(raw_input()) for i in range(n)]
	unit = min(book)
	max_shelf = max(sum(book)/m,max(book))
	while 1:
		i = 0
		cur_shelf,num = 0,1
		while i < n:
			if num > m: break
			if cur_shelf + book[i] > max_shelf:
				num += 1
				cur_shelf = 0
			else:
				cur_shelf += book[i]
				i += 1
		else:
			break
		max_shelf += unit
	print max_shelf

Fixed submission:
def isStorage(max_shelf):
	i,cur_shelf,num = 0,0,1
	while i < n:
		if num > m:
			return False
		if cur_shelf + book[i] > max_shelf:
			num += 1
			cur_shelf = 0
		else:
			cur_shelf += book[i]
			i += 1
	else:
		return True
			
while 1:
	m,n = map(int,raw_input().split())
	if m==0: break
	book = [int(raw_input()) for i in range(n)]
	max_shelf = len = sum(book)/2
	while len:
		len /= 2
		max_shelf -= len if isStorage(max_shelf) else -len
	max_shelf += 6
	while isStorage(max_shelf-1):
		max_shelf -= 1
	print max_shelf
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n=input()
	if n==0:break
	h=[0 for i in range(7)]
	for i in range(n):
		h[min(6,input()/10)]+=1
	for i in h:print i

Fixed submission:
while 1:
	n=int(raw_input())
	if n==0:break
	h=[0 for i in range(7)]
	for i in range(n):
		h[min(6,int(raw_input())/10)] += 1
	for i in h:print i
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	inp = raw_input()
	if inp == "0": break
	q1,b,c1,c2,q2 = map(int,inp.split())
	buy1 = min(b/c1,q2)
	buy2 = (b - buy1 * c1) / c2
	while buy1 * c1 + buy2 * c2 > b or buy1 + buy2 <q1:
		buy1 -= 1
		buy2 = (b - buy1 * c1) / c2
	if buy1 > 0:
		print buy1, buy2
	else:
		print "NA"

Fixed submission:
while 1:
	inp = raw_input()
	if inp == "0": break
	q1,b,c1,c2,q2 = map(int,inp.split())
	a1 = min(b/c1, q2)
	a2 = (b - a1*c1)/c2
	while a1 > 0  and (a1*c1 + a2*c2 > b or a1 + a2 < q1):
		a1 -= 1
		a2 = (b - a1*c1)/c2
	print "%d %d"%(a1,a2) if a1 > 0 else "NA"

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	inp = raw_input()
	if inp == "0": break
	q1,b,c1,c2,q2 = map(int,inp.split())
	buy1 = min(b / c1,q2)
	buy2 = (b - buy1 * c1) / c2
	while buy1 * c1 + buy2 * c2 > b or buy1 + buy2 <q1:
		buy1 -= 1
		buy2 = (b - buy1 * c1) / c2
		if buy1 == 0:
			print "NA"
	else:
		print buy1, buy2

Fixed submission:
while 1:
	inp = raw_input()
	if inp == "0": break
	q1,b,c1,c2,q2 = map(int,inp.split())
	a1 = min(b/c1, q2)
	a2 = (b - a1*c1)/c2
	while a1 > 0  and (a1*c1 + a2*c2 > b or a1 + a2 < q1):
		a1 -= 1
		a2 = (b - a1*c1)/c2
	print "%d %d"%(a1,a2) if a1 > 0 else "NA"
Buggy python submission (verdict: Runtime Error):
while 1:
	n = input()
	if n == 0: break
	N = [int(raw_input()) for i in range(n)]
	s = input()
	i = n/2
	c = 1
	while i > 0:
		c += 1
		if N[i] == s:
			break
		elif N[i] < s:
			i /= 2
		else:
			i += i/2
	print c

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	N = [input() for i in range(n)]
	s = input()
	l,r= 0,n-1
	c = 0
	while l <= r:
		i = (l + r)/2
		c += 1
		if N[i] == s:
			break
		elif N[i] < s:
			l = i + 1
		else:
			r = i - 1
	print c

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n = input()
	if n == 0: break
	N = [int(raw_input()) for i in range(n)]
	s = input()
	i = n/2
	c = 1
	while i > 0:
		c += 1
		if N[i] == s:
			break
		elif N[i] < s:
			i /= 2
		else:
			i = min(n-1,i+i/2)
	print c

Fixed submission:
while 1:
	n = input()
	if n == 0: break
	N = [input() for i in range(n)]
	s = input()
	l,r= 0,n-1
	c = 0
	while l <= r:
		i = (l + r)/2
		c += 1
		if N[i] == s:
			break
		elif N[i] < s:
			l = i + 1
		else:
			r = i - 1
	print c
Buggy python submission (verdict: Time Limit Exceeded):
inf = 0x101010
def floyd(P,m):
	for k in range(m):
		for i in range(m):
			for j in range(m):
				P[i][j]=min(P[i][j],P[i][k]+P[k][j])

while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	C = [[inf]*n for i in range(m)]
	T = [[inf]*n for i in range(m)]
	for i in range(m):
		C[i][i] = 0
		T[i][i] = 0
	for i in range(n):
		a,b,c,t = map(int,raw_input().split())
		C[a-1][b-1] = C[b-1][a-1] = c
		T[a-1][b-1] = T[b-1][a-1] = t
	floyd(C,m);floyd(T,m)
	for i in range(input()):
		p,q,r = map(int,raw_input().split())
		print C[p-1][q-1] if r == 0 else T[p-1][q-1]

Fixed submission:
inf = 0x10101010
def solve(A,strt):
	cost = [inf]*m
	visited = [False]*m
	cost[strt] = 0
	while 1:
		min = inf
		next = -1
		visited[strt] = True
		for i in range(m):
			if visited[i]: continue
			if A[strt][i]:
				d = cost[strt] + A[strt][i]
				if d < cost[i]:
					cost[i] = d
			if min > cost[i]:
				min = cost[i]
				next = i
		strt = next
		if next == -1: break
	return cost
    
while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	T = [[0]*m for i in range(m)]
	C = [[0]*m for i in range(m)]
	for i in range(n):
		a,b,c,t = map(int,raw_input().split())
		T[a-1][b-1] = T[b-1][a-1] = t
		C[a-1][b-1] = C[b-1][a-1] = c
	TS = [solve(T,i) for i in range(m)]
	CS = [solve(C,i) for i in range(m)]
	for i in range(input()):
		a,b,q = map(int,raw_input().split())
		if q == 0:
			print CS[a-1][b-1]
		else:
			print TS[a-1][b-1]