Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	sh=set([min(s[i]) for i in range(n)])
	tl=set([max([s[j][i] for j in range(n)]) for i in range(n)])
	print list(sh&tl)[0]
	

Fixed submission:
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	sh=set([min(s[i]) for i in range(n)])
	tl=set([max([s[j][i] for j in range(n)]) for i in range(n)])
	print list(sh&tl)[0] if len(sh&tl)>0 else 0

Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	sh=set([min(s[i]) for i in range(n)])
	tl=set([max([s[j][i] for j in range(n)]) for i in range(n)])
	print list(sh&tl)[0]

Fixed submission:
while 1:
	n=input()
	if n==0:break
	s=[map(int,raw_input().split()) for i in range(n)]
	sh=set([min(s[i]) for i in range(n)])
	tl=set([max([s[j][i] for j in range(n)]) for i in range(n)])
	print list(sh&tl)[0] if len(sh&tl)>0 else 0
Buggy python submission (verdict: Runtime Error):
while 1:
	N,K = map(int,raw_input().split())
	if N == 0: break
	S = map(int,raw_input().split())
	for j in range(N):
		B = map(int,raw_input().split())
		for i in range(K):
			S[i] -= B[i]
		if min(S) < 0: break
	print "Yes" if min(S) >= 0 else "No"

Fixed submission:
while 1:
	N,K = map(int,raw_input().split())
	if N == 0: break
	S = map(int,raw_input().split())
	B = [map(int,raw_input().split()) for i in range(N)]
	for breed in B:
		for i in range(K):
			S[i] -= breed[i]
	print "Yes" if min(S) >= 0 else "No"

Buggy python submission (verdict: Runtime Error):
while 1:
	N,K = map(int,raw_input().split())
	if N == 0: break
	S = map(int,raw_input().split())
	for j in range(N):
		B = map(int,raw_input().split())
		for i in range(K):
			S[i] -= B[i]
		if min(S) < 0: break
	print "Yes" if min(S) >= 0 else "No"

Fixed submission:
while 1:
	N,K = map(int,raw_input().split())
	if N == 0: break
	S = map(int,raw_input().split())
	B = [map(int,raw_input().split()) for i in range(N)]
	for breed in B:
		for i in range(K):
			S[i] -= breed[i]
	print "Yes" if min(S) >= 0 else "No"
Buggy python submission (verdict: Runtime Error):
while 1:
  k=input()
  if not k:stop
  print sum(map(int, raw_input().split()))/(k-1)

Fixed submission:
while 1:
  k=input()-1
  if k==-1:break
  print sum(map(int, raw_input().split()))/k

Buggy python submission (verdict: Runtime Error):
while 1:
  k=input()-1
  if k==0:break
  print sum(map(int, raw_input().split()))/k

Fixed submission:
while 1:
  k=input()-1
  if k==-1:break
  print sum(map(int, raw_input().split()))/k
Buggy python submission (verdict: Runtime Error):
while True:
    n = int(raw_input())
    if n == 0: break
    a = []
    for i in range(n/4):
        a.append(int(raw_input())
    print sum(a)

Fixed submission:
while True:
    n = int(raw_input())
    if n == 0: break
    a = []
    for i in range(n/4):
        a.append(int(raw_input()))
    print sum(a)
Buggy python submission (verdict: Runtime Error):
while 1:
	a=map(str,raw_input().replace("  "," A ").split())
	if a==["END"]:break
	print "".join(map(str,[len(a[i]) for i in range(len(a))]))

Fixed submission:
while 1:
    m=raw_input()
    if m=="END OF INPUT":break
    print "".join(str(len(w)) for w in m.split(" "))
Buggy python submission (verdict: Runtime Error):
upp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
low = "abcdefghijklmnopqrstuvwxyz"

while 1:
	a,type = raw_input().split()
	if type == "X": break
	ans = ""
	if type == "U":
		if "_" in a:
			words = a.split("_")
			for word in words:
				ans += upp[low.index(word[0])] + word[1:]
		else:
			ans = upp[low.index(a[0])] + a[1:]
	elif type == "L":
		if "_" in a:
			words = a.split("_")
			ans += words[0]
			for word in words[1:]:
				ans += upp[low.index(word[0])] + word[1:]
		else:
			ans = low[upp.index(a[0])] + a[1:]
	else:
		words = []
		sp = 0
		for i in range(1,len(a)):
			if a[i] in upp:
				words.append(a[sp:i].lower())
				sp = i
		words.append(a[sp:].lower())
		ans = "_".join(words)
	print ans

Fixed submission:
while 1:
	a,type = raw_input().split()
	if type == "X": break
	ans = ""
	if type == "U":
		for word in a.split("_"):
			ans += word[0].upper() + word[1:]
	elif type == "L":
		for word in a.split("_"):
			ans += word[0].upper() + word[1:]
		ans = ans[0].lower() + ans[1:]
	else:
		if "_" not in a:
			words = []
			sp = 0
			for i in range(1,len(a)):
				if a[i].isupper() :
					words.append(a[sp:i].lower())
					sp = i
			words.append(a[sp:].lower())
			ans = "_".join(words)
		else:
			ans = a
	print ans
Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	p=raw_input().split()+["end"]
	sp=p[0]
	for i in range(n):
		if str(int(p[i])+1)!=p[i+1]:
			print sp+"-"+p[i], if sp!=p[i] else sp,
			sp=p[i+1]
	print

Fixed submission:
while 1:
	n=input()
	if n==0:break
	p=raw_input().split()+["end"]
	sp=p[0]
	for i in range(n):
		if str(int(p[i])+1)!=p[i+1]:
			if sp!=p[i]:
				print sp+"-"+p[i],
			else:
				print sp,
			sp=p[i+1]
	print
Buggy python submission (verdict: Runtime Error):
while True:
    r, c = input(),input()
    if not r: break
    print "yes" if not r*c%2 else "no"

Fixed submission:
while 1:
    r,c=map(int,raw_input().split())
    if r==0:break
    print "yes" if r*c%2==0 else "no"

Buggy python submission (verdict: Runtime Error):
while True:
    r, c = input()
    if not r: break
    print "yes" if not r*c%2 else "no"

Fixed submission:
while 1:
    r,c=map(int,raw_input().split())
    if r==0:break
    print "yes" if r*c%2==0 else "no"
Buggy python submission (verdict: Runtime Error):
while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	p = map(int,raw_input().split())
	sp = set()
	for pi in p: sp |= set([pi*j for j in range(1,n/pi+1)])
	print (n*(n+1)/2-sum(sp))*1.0/(n-len(sp)) if n-len(sp) > 0 else 0

Fixed submission:
while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	p = min(map(int,raw_input().split()))
	print n/2.0 if p > 1 else 0.0

Buggy python submission (verdict: Runtime Error):
import itertools
def prdct(array):
	ref = 1
	for a in array: ref *= a
	return ref

while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	p = sorted(map(int,raw_input().split()))
	if 1 in p:
		print 0.0
		continue
	for i in range(len(p)-1,0,-1):
		for j in range(i):
			while 1:
				if p[i] == p[j]:
					del p[i]
					break
				elif p[i]%p[j] == 0:
					p[i] /= p[j]
				else:
					break
	d = sum(pi*(n/pi*(n/pi+1))/2 for pi in p)
	m = sum(n/pi for pi in p)
	q = []
	for i in range(2,len(p)+1):
		q += [prdct(element) for element in itertools.combinations(p,i)]
	d -= sum(qi*(n/qi*(n/qi+1))/2 for qi in q)
	m -= sum(n/qi for qi in q)
	print (n*(n+1)/2-d)*1.0/(n-m) if n-m > 0 else 0.0

Fixed submission:
while 1:
	n,m = map(int,raw_input().split())
	if n == 0: break
	p = min(map(int,raw_input().split()))
	print n/2.0 if p > 1 else 0.0
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
    math,greed,geo,dp,graph,other = map(int,raw_input().split())
    md,gg,go = math+dp,greed+graph,geo+other
    if md == gg == go == 0: break
    count = 0
    while md > 2 and gg > 2 and go > 2:
        md -= 1; gg -= 1; go -= 1
        count += 1
    print count + md/3 + gg/3 + go/3 + min(md%3,gg%3,go%3)

Fixed submission:
while 1:
	N = map(int,raw_input().split())
	if sum(N) == 0: break
	a,b,c = N[0]+N[3],N[1]+N[4],N[2]+N[5]
	m = min(a,b,c)
	ans = 0
	for i in range(max(0,m-2),m+1):
		ans = max(ans,i+(a-i)/3+(b-i)/3+(c-i)/3)
	print ans

Buggy python submission (verdict: Runtime Error):
while 1:
	N = map(int,raw_input().split())
	if sum(N) == 0: break
	md,gg,go = N[0]+N[3],N[1]+N[4],N[2]+N[5]
	mn = min(md,gg,go)
	ans = 0
	for i in range(max(0,w-2),w):
		ans = max(ans,i+(md-i)/3+(gg-i)/3+(go-i)/3)
	print ans

Fixed submission:
while 1:
	N = map(int,raw_input().split())
	if sum(N) == 0: break
	a,b,c = N[0]+N[3],N[1]+N[4],N[2]+N[5]
	m = min(a,b,c)
	ans = 0
	for i in range(max(0,m-2),m+1):
		ans = max(ans,i+(a-i)/3+(b-i)/3+(c-i)/3)
	print ans
Buggy python submission (verdict: Time Limit Exceeded):
def judge(exp,torf,i):
	if i==len(dic):
		return eval(exp)
	else:
		return judge(exp,torf+[True],i+1) or judge(exp,torf+[False],i+1)
while 1:
	exp=list(raw_input())
	if exp[0]=="#":break
	dic=[]
	for i in range(len(exp)):
		if exp[i]=="&":exp[i]="and"
		elif exp[i]=="~":exp[i]="not"
		elif exp[i]=="|":exp[i]="or"
		elif exp[i]!="(" and exp[i]!=")":
			if exp[i] not in dic:
				dic.append(exp[i])
				exp[i]="torf["+str(len(dic)-1)+"]"
			else:
				exp[i]="torf["+str(dic.index(exp[i]))+"]"
	exp=" ".join(map(str,exp))
	print "yes" if judge(exp,[],0) else "no"

Fixed submission:
while 1:
	exps=raw_input().split("|")
	if exps[0]=="#":break
	for exp in exps:
		v=1
		lit=exp[1:-1].split("&")
		for i in range(2):
			for j in range(i+1,3):
				if (lit[i]=="~"+lit[j]) or ("~"+lit[i]==lit[j]):
					v=0
		if v:
			print "yes"
			break
	else:
		print "no"
Buggy python submission (verdict: Runtime Error):
while 1:
	r,c,q = map(int,raw_input().split())
	if r == 0: break
	R = [[0,0] for i in range(r)]
	C = [[0,0] for i in range(r)]
	for i in range(1,q+1):
		A,B,order = map(int,raw_input().split())
		if A == 0:
			R[B] = [i,order]
		else:
			C[B] = [i,order]
	ans = 0
	for ri in range(r):
		ans += sum(R[ri][1] if R[ri][0] > C[ci][0] else C[ci][1] for ci in range(c))
	print ans

Fixed submission:
from bisect import *
while 1:
	r,c,q = map(int,raw_input().split())
	if r == 0: break
	R = [[0,0] for i in range(r)]
	C = [[0,0] for i in range(c)]
	for i in range(1,q+1):
		A,B,order = map(int,raw_input().split())
		if A == 0:
			R[B] = [i,order]
		else:
			C[B] = [i,order]
	CC =  [sorted(C[ci][0] for ci in range(c) if C[ci][1] == 1)]
	CC += [sorted(C[ci][0] for ci in range(c) if C[ci][1] == 0)]
	ans = r*sum(C[ci][1] for ci in range(c))
	ans -= sum((-1)**R[ri][1]*bisect(CC[R[ri][1]],R[ri][0]) for ri in range(r))
	print ans

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	r,c,q = map(int,raw_input().split())
	if r == 0: break
	R = [[0,0] for i in range(r)]
	C = [[0,0] for i in range(c)]
	for i in range(1,q+1):
		A,B,order = map(int,raw_input().split())
		if A == 0:
			R[B] = [i,order]
		else:
			C[B] = [i,order]
	ans = 0
	for ri in range(r):
		ans += sum(R[ri][1] if R[ri][0] > C[ci][0] else C[ci][1] for ci in range(c))
	print ans

Fixed submission:
from bisect import *
while 1:
	r,c,q = map(int,raw_input().split())
	if r == 0: break
	R = [[0,0] for i in range(r)]
	C = [[0,0] for i in range(c)]
	for i in range(1,q+1):
		A,B,order = map(int,raw_input().split())
		if A == 0:
			R[B] = [i,order]
		else:
			C[B] = [i,order]
	CC =  [sorted(C[ci][0] for ci in range(c) if C[ci][1] == 1)]
	CC += [sorted(C[ci][0] for ci in range(c) if C[ci][1] == 0)]
	ans = r*sum(C[ci][1] for ci in range(c))
	ans -= sum((-1)**R[ri][1]*bisect(CC[R[ri][1]],R[ri][0]) for ri in range(r))
	print ans
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	r = sorted([int(raw_input()) for i in range(n)], reverse = True)
	c = 0
	for i in range(n):
		if r[i] < S/2:
			break
		for j in range(i+1,n):
			if r[i]+r[j] > S:
				c += 1
			else:
				break
	print c

Fixed submission:
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	d = [int(raw_input()) for i in range(n)]
	r = [0]*R
	for i in set(d): r[i] = d.count(i)
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-i+1),R)])
	print ans

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	r = sorted([int(raw_input()) for i in range(n)], reverse = True)
	c = 0
	for i in range(n):
		if r[i] >= S:
			c += n-i-1
			continue
		elif r[i] < S/2:
			break
		for j in range(i+1,n):
			if r[i]+r[j] > S:
				c += 1
			else:
				break
	print c

Fixed submission:
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	d = [int(raw_input()) for i in range(n)]
	r = [0]*R
	for i in set(d): r[i] = d.count(i)
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-i+1),R)])
	print ans

Buggy python submission (verdict: Runtime Error):
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	r = [0]*R
	for i in range(n):
		r[int(raw_input())] += 1
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-j+1),R)])
	print ans

Fixed submission:
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	d = [int(raw_input()) for i in range(n)]
	r = [0]*R
	for i in set(d): r[i] = d.count(i)
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-i+1),R)])
	print ans

Buggy python submission (verdict: Runtime Error):
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	d = [int(raw_input()) for i in range(n)]
	r = [d.count(i) for i in set(d)]
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-i+1),R)])
	print ans

Fixed submission:
R = 101
while 1:
	n,S = map(int,raw_input().split())
	if n == 0: break
	d = [int(raw_input()) for i in range(n)]
	r = [0]*R
	for i in set(d): r[i] = d.count(i)
	ans = 0
	for i in range(R):
		if 2*i > S:
			ans += r[i]*(r[i]-1)/2
		ans += r[i]*sum([r[j] for j in range(max(i+1,S-i+1),R)])
	print ans
Buggy python submission (verdict: Runtime Error):
while 1:
	n,a,b = map(int,raw_input().split())
	if n == 0: break
	dp = [0]*(n+1)
	dp[a] = dp[b] = 1
	for i in range(n+1):
		if dp[i] == 1:
			if i+a < n+1: dp[i+a] = 1
			if i+b < n+1: dp[i+b] = 1
	print n - sum(dp)

Fixed submission:
while 1:
	n,a,b = map(int,raw_input().split())
	if n == 0: break
	S = [1]*(n+1)
	for i in range(a):
		s = b*i
		while s <= n:
			S[s] = 0
			s += a
	print sum(S)

Buggy python submission (verdict: Runtime Error):
while 1:
	n,a,b = map(int,raw_input().split())
	if n == 0: break
	dp = [0]*(max(n,a,b)+1)
	dp[a] = dp[b] = 1
	for i in range(n+1-min(a,b)):
		if dp[i]:
			dp[i+a] = dp[i+b] = 1
	print n - sum(dp[:n+1])

Fixed submission:
while 1:
	n,a,b = map(int,raw_input().split())
	if n == 0: break
	S = [1]*(n+1)
	for i in range(a):
		s = b*i
		while s <= n:
			S[s] = 0
			s += a
	print sum(S)
Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()**3
	if n==0:break
	mn=n
	for i in range(1,n):
		for j in range(i,n):
			m=n-i**3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Fixed submission:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((z3-x3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n=input()
	n3=n**3
	if n==0:break
	mn=n3
	i=j=0
	while 1:
		i+=1
		j=i
		if n3-i**3-j**3<0:break
		while 1:
			j+=1
			m=n3-i**3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Fixed submission:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((z3-x3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n=input()
	n3=n**3
	if n==0:break
	mn=n3
	i=j=0
	while 1:
		i+=1
		j=i
		i3=i**3
		if n3-i3-j**3<0:break
		while 1:
			j+=1
			m=n3-i3-j**3
			if m<0:break
			if m<mn:mn=m
	print mn

Fixed submission:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((z3-x3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m

Buggy python submission (verdict: Runtime Error):
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((n3-i3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m

Fixed submission:
while 1:
	z3=m=input()**3
	if z3==0:break
	x=0
	while 1:
		x+=1
		x3=x**3
		y=int((z3-x3)**(1/3.))
		if y<x:break
		n=z3-x3-y**3
		if n<m:m=n
	print m
Buggy python submission (verdict: Runtime Error):
import re
while 1:
	n,m = map(int,raw_input().split())
	P = []; D = []
	for i in range(n):
		p,s,d = raw_input().replace("?","[0-9]").split()
		r = re.compile(s+d)
		if p[0] == "p":
			P.append(r)
		else:
			D.append(r)
	for i in range(m):
		s,d,name = raw_input().split()
		tf = any(r.search(s+d) for r in P) and not any(r.search(s+d) for r in D)
		if tf:
			print s,d,name

Fixed submission:
import re
while 1:
	n,m = map(int,raw_input().split())
	if n == m == 0: break
	R = [0]*n
	for i in range(n):
		p,s,d = raw_input().replace("?","[0-9]").split()
		R[i] = [re.compile(s+d), p[0] == "p"]
	ans = []
	for i in range(m):
		s,d,name = raw_input().split()
		for r,p in R[::-1]:
			if r.search(s+d):
				if p: ans.append([s,d,name])
				break
	print len(ans)
	for s,d,name in ans:
		print s,d,name
Buggy python submission (verdict: Time Limit Exceeded):
def rec(h,w,s):
    res = 0
    for dh,dw in [[1,0],[0,1]]:
        nh,nw = h+dh,w+dw
        if 0 <= nh < H and 0 <= nw < W and C[nh][nw].isdigit():
            res = max(res, rec(nh,nw,s+C[nh][nw]))
    return res if res > 0 else int(s)

while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    C = [list(raw_input()) for _ in xrange(H)]
    ans = 0
    for h in xrange(H):
        for w in xrange(W):
           if C[h][w].isdigit():
               ans = max(ans, rec(h,w,C[h][w]))
    print ans

Fixed submission:
while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    C = [list(raw_input()) for _ in xrange(H)]
    dp = [[0]*W for _ in xrange(H)]
    ans = 0
    for h in xrange(H):
        for w in xrange(W):
            if C[h][w].isdigit():
                dp[h][w] = max(dp[h][w],int(C[h][w]))
                if w > 0: dp[h][w] = max(dp[h][w],int(str(dp[h][w-1])+C[h][w]))
                if h > 0: dp[h][w] = max(dp[h][w],int(str(dp[h-1][w])+C[h][w]))
    print max(max(i) for i in dp)

Buggy python submission (verdict: Time Limit Exceeded):
def rec(h,w,s):
    res = 0
    for dh,dw in [[1,0],[0,1]]:
        nh,nw = h+dh,w+dw
        if 0 <= nh < H and 0 <= nw < W and C[nh][nw].isdigit():
            res = max(res, rec(nh,nw,s+C[nh][nw]))
    return res if res > 0 else int(s)

while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    C = [list(raw_input()) for _ in xrange(H)]
    ans = 0
    for h in xrange(H):
        for w in xrange(W):
           if C[h][w].isdigit():
               if w > 0 and C[h][w-1].isdigit() or h > 0 and C[h-1][w].isdigit(): continue
               ans = max(ans, rec(h,w,C[h][w]))
    print ans

Fixed submission:
while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    C = [list(raw_input()) for _ in xrange(H)]
    dp = [[0]*W for _ in xrange(H)]
    ans = 0
    for h in xrange(H):
        for w in xrange(W):
            if C[h][w].isdigit():
                dp[h][w] = max(dp[h][w],int(C[h][w]))
                if w > 0: dp[h][w] = max(dp[h][w],int(str(dp[h][w-1])+C[h][w]))
                if h > 0: dp[h][w] = max(dp[h][w],int(str(dp[h-1][w])+C[h][w]))
    print max(max(i) for i in dp)
Buggy python submission (verdict: Time Limit Exceeded):
import copy
while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    field = [map(int,raw_input().split()) for _ in range(H)]
    for y in range(H):
        for x in range(W):
            if field[y][x] == 2: sx,sy = x,y

    que = [[sx,sy,10,field[:]]]
    while que:
        x,y,t,field = que.pop(0)
        if t == -1: continue
        if field[y][x] == 3:
            print 10-t
            break
        for dx,dy in zip([1,0,-1,0],[0,1,0,-1]):
            _field = copy.deepcopy(field[:])
            nx,ny = x+dx,y+dy
            while 0 <= nx < W and 0 <= ny < H:
                if _field[ny][nx] == 3:
                    que.append([nx,ny,t-1,_field[:]])
                    break
                if _field[ny][nx] == 1:
                    _field[ny][nx] = 0
                    if abs(nx-x)+abs(ny-y) == 1: break
                    que.append([nx-dx,ny-dy,t-1,_field[:]])
                    break
                nx += dx; ny += dy
    else:
        print -1

Fixed submission:
dxy = zip([1,0,-1,0],[0,1,0,-1])
def rec(x,y,t):
    global ans
    if t >= ans: return
    for dx,dy in dxy:
        nx,ny = x+dx,y+dy
        while 0 <= nx < W and 0 <= ny < H:
            if   field[ny][nx] == 3: ans = t
            elif field[ny][nx] == 1:
                if abs(nx-x)+abs(ny-y) == 1: break
                field[ny][nx] = 0
                rec(nx-dx,ny-dy,t+1)
                field[ny][nx] = 1
                break
            nx += dx; ny += dy

while 1:
    W,H = map(int,raw_input().split())
    if W == 0: break
    field = [map(int,raw_input().split()) for _ in range(H)]
    for y in range(H):
        for x in range(W):
            if field[y][x] == 2: sx,sy = x,y
    ans = 11
    rec(sx,sy,1)
    print ans if ans < 11 else -1
Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for m in range(M):
		c = 0
		for i in range(1260):
			if T[m-1][i]:
				c += 1
			T[m-1][i] = c - T[m-1][i+1]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print T[m-1][te] - T[m-1][ts]

Fixed submission:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(T[m-1][ts:te])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for m in range(M):
		c = 0
		for i in range(540,1260):
			if T[m-1][i]:
				c += 1
			T[m-1][i] = c - T[m-1][i+1]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print T[m-1][te] - T[m-1][ts]

Fixed submission:
while 1:
	N,M = map(int,raw_input().split())
	if N == 0: break
	T = [[0]*1261 for i in range(M)]
	r = input()
	tnms = sorted([map(int,raw_input().split()) for i in range(r)], key = lambda x:x[1])
	for i in range(0,r,2):
		t1,n,m,s = tnms[i]
		t2,n,m,s = tnms[i+1]
		T[m-1][t1:t2] = [1 for j in range(t1,t2)]
	for i in range(input()):
		ts,te,m = map(int,raw_input().split())
		print sum(T[m-1][ts:te])
Buggy python submission (verdict: Runtime Error):
def solve(a):
	if type(a[0]) is int:
		return sum(sorted(a)[:len(a)/2+1])/2 + len(a)
	else:
		return sum(sorted(solve(i) for i in a))[:len(a)/2+1])
for i in range(input()):
	A = eval(raw_input().replace("][","],["))
	print solve(A)

Fixed submission:
def solve(a):
	if type(a[0]) is int:
		return sum(sorted(a)[:len(a)/2+1])/2 + len(a)
	else:
		return sum(sorted(solve(i) for i in a)[:len(a)/2+1])
for i in range(input()):
	A = eval(raw_input().replace("][","],["))
	print solve(A)