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
}

C++: //#define LOCAL

#include <fstream>
#include <iostream>
#include <cmath>
#include <algorithm>

#define rep(n) for(int i=0; i<n; i++)

using namespace std;

int n;
int a[30];
int ans;

void rec(int i, int A, int B)
{
    if (i == n) {
        ans = min(ans, abs(A - B));
        return;
    }

    rec(i + 1, A + a[i], B);
    rec(i + 1, A, B + a[i]);
}

int solve()
{
    ans = 30000000;
    rec(0, 0, 0);
    return ans;
}

int main()
{
#ifdef LOCAL
    ifstream in("input.txt");
    cin.rdbuf(in.rdbuf());
#endif

    while (true) {
        cin >> n;
        if (n == 0) break;

        rep(n) {
            cin >> a[i];
        }

        cout << solve() << endl;
    }
}

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

using namespace std;

int main( void )
{
	int n;
	int teamA, teamB, map[20];
	while(cin >> n, n) {
		for (int i = 0; i < n; ++i) cin >> map[i];
		sort(map, map+n, greater<int>());
		teamA = teamB = 0;
		for (int i = 0; i < n; ++i) {
			if(teamA > teamB)	teamB += map[i];
			else				teamA += map[i];
		}
		cout << abs(teamA - teamB) << endl;
	}
	return 0;
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> d(n);
		long long int a = 0;
		for (int i= 0; i < n; i++) {
			cin >> d[i];
			a += d[i];
		}
		int l = 1<<n;
		long long int MIN = a;
		for (int i = 0; i < l; i++) {
			long long int b = 0;
			for (int j = 0; j < n; j++) {
				if ((i & (1<<j)) != 0) {
					b += d[j];
				}
			}
			MIN = min(MIN, abs(abs(a-b)-b));
		}
		cout << MIN << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int n, m[20];
vector<int>X1;
vector<int>X2;
int Mid = 0;

int main() {
	while (true) {
		X1.clear();
		X2.clear();
		int sum = 0;
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
			sum += m[i];
		}
		Mid = n / 2;
		int s = 0;
		for (int i = 0; i < (1 << Mid); i++) {
			s = 0;
			for (int j = 0; j < Mid; j++) {
				if ((i / (1 << j)) % 2 == 0) {
					s += m[j];
				}
			}
			X1.push_back(s);
		}
		for (int i = 0; i < (1 << (n - Mid)); i++) {
			s = 0;
			for (int j = 0; j < (n - Mid); j++) {
				if ((i / (1 << j)) % 2 == 0) {
					s += m[Mid + j];
				}
			}
			X2.push_back(s);
		}
		int minx = 1000000000;
		sort(X2.begin(), X2.end());
		for (int i = 0; i < X1.size(); i++) {
			if (X1[i] <= sum / 2) {
				int target = sum / 2 - X1[i];
				int p = upper_bound(X2.begin(), X2.end(), target) - X2.begin();
				p--;
				for (int j = 0; j < 3; j++) {
					if (p >= 0 && p < X2.size()) {
						int a = X2[p] + X1[i];
						int b = sum - a;
						minx = min(minx, abs(a - b));
					}
					p++;
				}
			}
		}
		cout << minx << endl;
	}
	return 0;
}

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

using namespace std;


int a[100] = {0};
int minimum = 100000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}

int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

    for(int i = 0;i < n;i++){
      cin >> a[i];
      sum += a[i];
    }

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

int main(){
	int n,limit,diff_min,A,B;

	while(true){
		scanf("%d",&n);
		if(n == 0)break;

		int array[n];

		for(int i=0; i < n; i++){
			scanf("%d",&array[i]);
		}

		diff_min = 2147483647;
		limit = pow(2,n);

		for(int i=0; i < limit; i++){
			A = 0,B=0;
			for(int k=0; k < n; k++){
				if((i & (1 << k))){
					A += array[k];
				}else{
					B += array[k];
				}
			}
			if(diff_min > abs(A-B)){
				diff_min = abs(A-B);
			}
		}

		printf("%d\n",diff_min);

	}

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20],s1[1<<10];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int n2=n/2;
		fill(s1,s1+(1<<n2),0);
		int sum=0,dif=1e+9;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		if(n<3){
			fill(s,s+(1<<n),0);
			for(int i=0;i<1<<n;i++){
				int sss=0;
				for(int j=0;j<n;j++)if((i/(1<<j))%2)sss+=p[j];
				dif=min(dif,abs(sss*2-sum));
			}
			cout<<dif<<endl;
		}
		else{
		for(int i=0;i<1<<n2;i++){
			for(int j=0;j<n2;j++)if(i>>j&1)s1[i]+=p[j];
		}
		sort(s1,s1+(1<<n2));
		int ans=1e+9;
		for(int i=0;i<1<<(n-n2);i++){
			int ss=0;
			
			for(int j=0;j<n-n2;j++)if(i>>j&1)ss+=p[j+n2];
			ans=min(ans,min(abs((*lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)-1)+ss)*2-sum)));
		}
		cout<<ans<<endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> as(n);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            cin >> as[i];
            sum += as[i];
        }
        int ans = 1 << 29;
        for (int i = 0, size = 1 << (n-1); i < size; i++) {
            vector<int> gs;
            for (int j = 0; j < n-1; j++) {
                if (i & (1 << j)) {
                    gs.push_back(j);
                }
            }
            if (gs.empty()) {
                continue;
            }
            int a = 0;
            for (vector<int>::iterator it = gs.begin(); it != gs.end(); it++) {
                a += as[*it];
            }
            ans = min(ans, abs(sum - 2 * a));
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int main()
{
    int n, sum; vector<int> a;
    
    while(true)
    {
        cin >> n;
        
        if(n == 0) { break; }
        
        a = vector<int>(n); sum = 0;
        
        for(int i = 0; i < n; i++)
        {
            cin >> a[i]; sum += a[i];
        }
        
        int min_ = (1 << 29);
        
        for(int i = 0; i < (1 << n); i++)
        {
            int sum2 = 0;
            
            for(int j = 0; j < n; j++)
            {
                if(i & (1 << j))
                {
                    sum2 += a[j];
                }
            }

            min_ = min(abs(2 * sum2 - sum), min_);
        }
        
        cout << min_ << endl;
    }
    
    return 0;
}

C++: void dfs(int now, int i);

int a[100];
int minimum = 1000000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

    for(int i = 0;i < n;i++){
      cin >> a[i];
      sum += a[i];
    }

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}