Go: package main

import "fmt"

func main() {
	ans := make([]int, 0)
	for {
		var n int
		fmt.Scan(&n)
		if n == 0 {
			break
		}
		a := make([]int, n)
		for i := range a {
			fmt.Scan(&a[i])
		}
		mind := int(1e9)
		for bits := 0; bits < (1 << uint(n)); bits++ {
			gsum, psum := 0, 0
			for j := 0; j < n; j++ {
				if (bits & (1 << uint(j))) != 0 {
					gsum += a[j]
				} else {
					psum += a[j]
				}
			}
			mind = min(mind, abs(gsum-psum))
		}
		ans = append(ans, mind)
	}
	for i := range ans {
		fmt.Println(ans[i])
	}
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

Python: while True:
    n = int(input())
    if n == 0:
        break
    a = list(map(int, input().split()))
    b = sum(a)
    ans =  b >> 1
    if ans in a:
        print(b - 2*ans)
        continue
    c = [v for v in a if v < ans]
    s , f, q, qlen = set(), True, [0], 1
    for v in c:
        i, k = 0, qlen
        while k:
            v2 = v + q[i]
            k, i = k-1, i+1
            if v2 == ans:
                f = False
                break
            if v2 < ans and v2 not in s:
                s.add(v2)
                q.append(v2)
                qlen += 1
                
    if f:
        while True:
            ans -= 1
            if ans == 0:
                break
            if ans in s:
                break
    print(b - 2 * ans)

Python: m=[0]*20
d=1<<20
def f(a, b, c, n,):
    global d
    if c==n:d=min(d,abs(a-b));return
    f(a+m[c],b,c+1,n)
    f(a,b+m[c],c+1,n)
    return (d)
while 1:
    n=int(input())
    if n==0:break
    m=sorted(map(int,input().split()),reverse=1)
    print(f(0,0,0,n))

Python: while 1:
    n=int(input())
    if n==0:break
    m=sorted(map(int,input().split()),reverse=1)
    a=b=0
    print(m)
    for i in m:
        if a>b:b+=i
        else: a+=i
    print(abs(a-b))

Python: # AOJ 1045: Split Up!
# Python3 2018.7.6 bal4u

while True:
	n = int(input())
	if n == 0: break
	t = list(map(int, input().split()))
	s = sum(t)
	ans = s >> 1
	if ans in t:
		print(s - 2*ans)
		continue
	a = [v for v in t if v < ans]
	S, f, Q, qlen = set(), True, [0], 1
	for v in a:
		i, k = 0, qlen
		while k:
			v2 = v + Q[i]
			k, i = k-1, i+1
			if v2 == ans:
				f = False
				break
			if v2 < ans and v2 not in S:
				S.add(v2)
				Q.append(v2)
				qlen += 1
	if f:
		while True:
			ans -= 1
			if ans == 0: break
			if ans in S: break;
	print(s - 2*ans)

Python: while 1:
    n=int(input())
    if n==0:break
    m=sorted(map(int,input().split()),reverse=1)
    a=b=0
    for i in m:
        if a>b:b+=i
        else: a+=i
    print(abs(a-b))

Python: def main():
  while True:
    n = int(input())
    if n == 0:
      break
    alst = list(map(int, input().split()))
    s = sum(alst)
    alst = [a * 2 for a in alst]
    lst = [-s]
    for a in alst:
      lst.extend([i + a for i in lst])
    print(min(map(abs, lst)))
main()

Python: # AOJ 1045: Split Up!
# Python3 2018.7.6 bal4u

while True:
	n = int(input())
	if n == 0: break
	t = list(map(int, input().split()))
	s = sum(t)
	ans = s >> 1
	a, f = [], True
	for v in t:
		if v == ans:
			f = False
			break
		if v < ans: a.append(v)
	if f:
		S, Q = set(), [0]
		for v in a:
			while Q:
				v2 = v + Q.pop(0)
				if v2 == ans:
					f = False
					break
				if v2 < ans and v2 not in S:
					S.add(v2)
					Q.append(v2)
	if f:
		while ans > 1:
			ans -= 1
			if ans in S: break;
	print(s - 2*ans)

C++: #include <iostream>
#include <algorithm>

using namespace std;

int RegNum;//登録者数
int RegATK[20];//登録者戦闘力
int RegATKAll;//登録者戦闘力合計
int PartyATK[2];//パーティー戦闘力

int GetMinATK(int n,int PAATK){
	if (n == 0)return abs(RegATKAll - (2 * PAATK));
	return min(GetMinATK(n - 1, PAATK + RegATK[n]), GetMinATK(n - 1, PAATK));
}

int main(){

	while (1){
		//変数初期化
		PartyATK[0] = 0; PartyATK[1] = 0;
		RegATKAll = 0;

		cin >> RegNum;//登録者数代入
		if (RegNum == 0)break;//0ならbreak
		for (int i = 0; i < RegNum; ++i){//戦闘力代入
			cin >> RegATK[i];
			RegATKAll += RegATK[i];
		}

		cout << GetMinATK(RegNum-1, 0) << endl;
	}

	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int dp[2][20000001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0, maxi = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
			maxi = max(maxi, a[i]);
		}
		int k = 0;
		dp[k][abs(sum-2*a[0])] = 1;
		REP(i, 1, n) {
			rep(j, maxi+1) if (dp[k][abs(sum-2*j)]) {
				dp[1-k][abs(sum-2*(j+a[i]))] = 1;
				dp[1-k][abs(sum-2*j)] = 1;
			}
			k = 1 - k;
		}
		rep(i, maxi+1) if (dp[k][i]) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}

#if 0
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}
#endif

C++: #include<bits/stdc++.h>
#define mk make_pair
using namespace std;
int a[20],n;
int dfs(int d,int x,int y){
  if(d==n){if(y<x)swap(x,y);return y-x;}
  return min(dfs(d+1,x+a[d],y),dfs(d+1,x,y+a[d]));
}
int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i];
    cout<<dfs(0,0,0)<<endl;
  }
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> Vec;
int n;
int Ans;
static const int INF = 1e+9;

void DFS(int a, int b, int depth)
{
	if(depth == n)
	{
		Ans = min(Ans, abs(a - b));
	}
	else
	{
		DFS(a + Vec[depth], b, depth + 1);
		DFS(a, b + Vec[depth], depth + 1);
	}
}

void solve()
{
	while(cin >> n, n)
	{
		Vec.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		Ans = INF;
		DFS(0, 0, 0);
		cout << Ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>

using namespace std;

vector<int> ans;
int main(){
	while(1){
		int n;
		cin >> n;
		
		if(n == 0) break;
		
		int a = 0,b = 0;
		int nl[20];
		for(int i = 0;i < n;i++){
			cin >> nl[i];
		}
		
		sort(nl,nl+n);
		reverse(nl,nl+n);
		
		for(int i = 0;i < n;i++){
			if(a > b){
				b += nl[i];
			}else{
				a += nl[i];
			}
		}
		ans.push_back(abs(a-b));
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	
	return 0;
}

C++: #include<iostream>
#include<climits>

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int mi=INT_MAX;
        for(int l=0;l<(2<<n);l++){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
        }
        cout<<mi<<endl;
    }
    
}