Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = map(int,raw_input().split())
	sm = ans = sum(s)
	for i in range(1,n-1):
		for party in itertools.combinations(s,i):
			ans = min(ans,abs(sm-2*sum(party)))
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = map(int,raw_input().split())
	sm = ans = sum(s)
	for i in range(1,n-1):
		for party in itertools.combinations(s,i):
			ans = min(ans,abs(sm-2*sum(party)))
			if ans == 0:
				break
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm1 = ans = sum(s)
	for i in range(1,n-1):
		for party in sorted(itertools.combinations(s,i), key = lambda x:sum(x)):
			sm2 = sm1 - 2*sum(party)
			if sm2 < -ans: break
			ans = min(ans,abs(sm2))
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm1 = ans = sum(s)
	for i in range(1,n-1):
		for party in sorted(itertools.combinations(s,i), key = lambda x:sum(x)):
			sm2 = sm1 - 2*sum(party)
			if sm2 < -ans: break
			ans = min(ans,abs(sm2))
			if ans == 0: break
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Runtime Error):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm = sum(s)
	for i in range(1,n-1):
		ans = sorted(abs(sm - 2*sum(party)) for party in itertools.combinations(s,i))[0]
		if ans == 0: break
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Runtime Error):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm = sum(s)
	for i in range(1,n-1):
		ans = sorted(abs(sm - 2*sum(party)) for party in itertools.combinations(s,i))[0]
		if ans == 0: break
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	try:
		n = input()
		if n == 0: break
		s = sorted(map(int,raw_input().split()))
		sm = sum(s)
		for i in range(1,n-1):
			ans = sorted(abs(sm - 2*sum(party)) for party in itertools.combinations(s,i))[0]
			if ans == 0: break
		print ans
	except:
		pass

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm = ans = sum(s)
	for i in range(1,n-1):
		for party in sorted(itertools.combinations(s,i)):
			sm1 = sm - 2*sum(party)
			if sm1 < -ans: break
			ans = min(ans,abs(sm1))
			if ans == 0: break
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	s = sorted(map(int,raw_input().split()))
	sm = ans = sum(s)
	for i in range(1,n/2+1):
		for party in itertools.combinations(s,i):
			cur = sm - 2*sum(party)
			ans = min(ans,abs(cur))
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n = input()
	if n == 0: break
	s = map(int,raw_input().split())
	sm = sum(s)
	dp = [0] * (sm+1)
	ref = {i:[i] for i in s}
	for i in s:
		dp[i] = i
	for i in s:
		for j in range(sm+1):
			if dp[j] > 0 and j not in ref[i]:
				try:
					dp[j+i] = dp[j] + i
					ref[i].append(j + i)
				except: break
	print min([abs(sm-2*dp[i]) for i in range(sm)])

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))
	all = sum(a)
	ans = all
	for i in range((n+1)/2+1):
		c = 0
		for party in itertools.combinations(a,i):
			d = all-2*sum(party)
			if c == 0 and d < 0 and abs(d) > ans:
				break
			ans = min(ans,abs(d))
			c = 1
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Runtime Error):
import itertools
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))
	all = sum(a)
	ans = all
	for i in range((n+1)/2+1):
		c = 0
		for party in itertools.combinations(a,i):
			d = all-2*sum(party)
			if c == 0 and d < -ans
				break
			ans = min(ans,abs(d))
			c = 1
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))
	all = sum(a)
	ans = all
	for i in range((n+1)/2+1):
		c = 0
		for party in itertools.combinations(a,i):
			d = all-2*sum(party)
			if c == 0 and d < -ans:
				break
			ans = min(ans,abs(d))
			c = 1
	print ans

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
def solve(a,i,sa,sb):
	if 2*sa >= all: return 2*sa-all
	if 2*sb >= all: return 2*sb-all
	return min(solve(a,i+1,sa+a[i],sb),solve(a,i+1,sa,sb+a[i]))
	
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))
	all = sum(a)
	print solve(a,0,a[0],0)

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n = input()
	if n == 0: break
	a = map(int,raw_input().split())
	ref = []
	for i in a:
		ref += [i+j for j in ref]
		ref.append(i)
	print min(abs(sum(a)-2*i) for i in ref)

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n = input()
	if n == 0: break
	a = map(int,raw_input().split())
	ref = [0]
	for i in a:
		ref += [i+j for j in ref]
	print min(abs(sum(a)-2*i) for i in ref)

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Runtime Error):
while 1:
	n = input()
	if n == 0: break
	a = map(int,raw_input().split())
	ref = [0]
	for i in a:
		ref += [i+j for j in ref if j < ans/2]
	print min(abs(sum(a)-2*i) for i in ref)

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Buggy python submission (verdict: Runtime Error):
def S(i,x,y):
	for w in [x,y]
		if 2*w >= A: return 2*w-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)

Fixed submission:
def S(i,x,y):
	if 2*x >= A: return 2*x-A
	if 2*y >= A: return 2*y-A
	return min(S(i+1,x+a[i],y),S(i+1,x,y+a[i]))
while 1:
	n = input()
	if n == 0: break
	a = sorted(map(int,raw_input().split()))[::-1]
	A = sum(a)
	print S(1,a[0],0)