while 1:

	n = eval(input())

	if n == 0: break

	a = sorted(map(int,input().split()))[::-1]

	s = sum(a)

	L = [0]

	for i in a:

		L += [i+j for j in L if j < s/2]

	print(min(abs(s-2*i) for i in L))
def S(a,i,x,y):

	if 2*x >= A: return 2*x-A

	if 2*y >= A: return 2*y-A

	return min(S(a,i+1,x+a[i],y),S(a,i+1,x,y+a[i]))

while 1:

	n = eval(input())

	if n == 0: break

	a = sorted(map(int,input().split()))[::-1]

	A = sum(a)

	print(S(a,1,a[0],0))
while 1:
    n = eval(input())
    if n == 0:
        break
    a = sorted(map(int, input().split()))[::-1]
    s = sum(a)
    L = [0]
    for i in a:
        L += [i + j for j in L if j < s / 2]
    print(min(abs(s - 2 * i) for i in L))

def S(a, i, x, y):
    if 2 * x >= A:
        return 2 * x - A
    if 2 * y >= A:
        return 2 * y - A
    return min(S(a, i + 1, x + a[i], y), S(a, i + 1, x, y + a[i]))


while 1:
    n = eval(input())
    if n == 0:
        break
    a = sorted(map(int, input().split()))[::-1]
    A = sum(a)
    print(S(a, 1, a[0], 0))

+def S(a, i, x, y): +    if 2 * x >= A: +        return 2 * x - A +    if 2 * y >= A: +        return 2 * y - A +    return min(S(a, i + 1, x + a[i], y), S(a, i + 1, x, y + a[i])) + + -    s = sum(a) -    L = [0] -    for i in a: -        L += [i + j for j in L if j < s / 2] -    print(min(abs(s - 2 * i) for i in L)) +    A = sum(a) +    print(S(a, 1, a[0], 0))