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++: #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][21];
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];
			dp[0][i] = dp[1][i] = 1000000000;
		}
		int k = 0;
		rep(i, n) {
			dp[k][i] = sum - 2*a[i];
			rep(j, i) {
				if (dp[1-k][j] > 0 && dp[1-k][j] > abs(dp[1-k][j]-2*a[i])) {
					if (abs(dp[k][j]) > abs(dp[1-k][j] - 2*a[i]))
						dp[k][j] = dp[1-k][j] - 2*a[i];
				}
				else if (abs(dp[k][j]) > abs(dp[1-k][j])) {
					dp[k][j] = dp[1-k][j];
				}
			}
			k = 1 - k;
		}
		int res = 1000000000;
		rep(i, n) res = min(res, abs(dp[1-k][i]));
		cout << res << endl;
	}
	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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	int n;
	while(cin>>n&&n!=0){
		int a[20];
		int sum=0;
		for(int i=0; i < n; i++){
			cin >> a[i];
			sum+=a[i];
		}
		sort(a,a+n);

		int Asum=0;
		vector<int>A,B;
		int minSum;
		for(int i = 0; i < n; i++){
			minSum = abs(Asum-sum);
			if(Asum < sum){
				A.push_back(a[i]);
				Asum+=a[i];
				sum-=a[i];
			}
			else{
				for(int j = i; j < n; j++){
					B.push_back(a[j]);
				}
				break;
			}
		}
		minSum = abs(Asum-sum);
		for(int i = 0; i < A.size(); i++){
			if(Asum <= sum)
				break;
			Asum-=A[i];
			sum+=A[i];
			minSum = min(minSum,abs(Asum-sum));
		}
		cout << minSum << endl;
	}

	return 0;
}

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

using namespace std;

long min_p;
int n;
int a[21];

void dfs(int i, long pon, long gon) 
{
    if (i == n) {
        min_p = min(min_p, abs(pon - gon));
    }
    else {
        dfs(i + 1, pon + a[i], gon);
        dfs(i + 1, pon, gon + a[i]);
    }
}

int main()
{
    while (cin >> n, n != 0) {
        min_p = 20000000;

        for (int i = 0; i < n; i++)
            cin >> a[i];
        
        dfs(0, 0, 0);
        cout << min_p << endl;
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define mk make_pair
using namespace std;
int a[20],n;
map<pair<pair<int,int>,int>,int >m;
int dfs(int d,int x,int y){
  if(d==n){if(y<x)swap(x,y);return y-x;}
  if(m[mk(mk(x,y),d)])return m[mk(mk(x,y),d)];
  return (m[mk(mk(x,y),d)])=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;m.clear();
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define PI 4*atan(1)
#define INF 1e8

typedef long long ll;

int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};

int N;
ll dfs(int n, vector<ll> &a, ll pon, ll gon){
  ll res = abs(pon - gon);
  if(n == N)return res;
  return min(dfs(n + 1, a, pon + a[n], gon), dfs(n + 1, a, pon, gon + a[n]));
}

int main(){
  while(cin >> N, N){
    vector<ll> a(N);
    for(int i = 0; i < N; i++)cin >> a[i];
    cout << dfs(0, a, 0, 0) << endl;  
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

int rec(int n, int A, int B, vector<int> &a)
{
    if (n == (int)a.size()) {
        return abs(A - B);
    }

    return min(rec(n + 1, A + a[n], B, a),
               rec(n + 1, A, B + a[n], a));
}

int main()
{
    int N;
    while (cin >> N, N) {
        vector<int> a(N);
        for (int i = 0; i < N; i++) {
            cin >> a[i];
        }
        cout << rec(0, 0, 0, a) << endl;
    }
    return 0;
}

C++: 
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
#include <cctype>
#include <complex>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;



bool solve(){
	while(1){
		int n;
		cin>> n;
		if(!n) break;
		
		vector<int> a(n);
		int sum=0;
		for(int i=0;i<n;i++){
			cin>> a[i];
			sum += a[i];
		}

		sort(a.begin(), a.end());
//		int lsum,hsum;
//		lsum = hsum = 0;
		int sum2 = sum;
		vector<vector<int> > hoge(n+1, vector<int>(sum+1, 0));
		hoge[0][0] = 1;
		for(int i=0;i<n;i++){
			for(int j=0;j<=sum;j++){
				hoge[i+1][j] = hoge[i][j];
				if(j>=a[i] && hoge[i][j-a[i]]){
					hoge[i+1][j] = 1;
					if(abs(j-sum/2) < abs(sum2-sum/2)) sum2 = j;
					if(sum%2 &&  abs(j-sum/2+1) < abs(sum2-sum/2+1)) sum2 = j;
				}
//				cout<< hoge[i+1][j];
			}
//			cout<< "   "<< sum2;
//			cout<< endl;
			if(sum2 == sum/2) break;
		}
		int ans = abs(sum2 - (sum-sum2));
		cout<< ans<< endl;
	}
	return true;
}



int main(){
	cout.setf(ios::fixed);
	cout.precision(10);
	solve();

	return 0;
}

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

using namespace std;

int a[21];
int n;

int solve(int p, int s, int t)
{
  if(p == n) return abs(s-t);
  return min(solve(p+1, s+a[p], t), solve(p+1, s, t+a[p]));
}

int main()
{
  while(scanf("%d", &n)!=EOF && n != 0){
    for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
    printf("%d\n", solve(0, 0, 0));
  }

  return 0;
}

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);
        
        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;
}