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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int n,s,a[20];
int dfs(int i, int sum){
  if(i==n) return abs(s-sum*2);
  if((sum+a[i])*2>s) return dfs(i+1,sum);
  return min( dfs(i+1,sum+a[i]), dfs(i+1,sum));
}
int main(void){
  while(cin>>n){
    if(n==0) break;
    REP(i,n) cin>>a[i];
    s = accumulate(a,a+n,0);
    cout<<dfs(0,0)<<endl;
  }
  return 0;
}

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

int n, m[20];

int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> m[i];
		}
		int a = 1 << n, b, c, d, minx = 1000000000;
		for (int i = 0; i < a; i++) {
			b = 0; c = 0; d = 0;
			for (int j = 1; j < a; j *= 2) {
				if ((i / j) % 2 == 0) {
					b += m[d];
				}
				else {
					c += m[d];
				}
				d++;
			}
			minx = min(minx, abs(b - c));
		}
		cout << minx << endl;
	}
	return 0;
}

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

int n,deta[30],kon,pon;
int sa = 1000000;
int max;
void flat(int joutai,int kon,int pon);

using namespace std;
int main(void)
{
	while(scanf("%d",&n))
	{
		max = 0;
		sa = 1000000;
		if(n == 0) break;
		for(int i = 1;i<=n;i++)
		{
			scanf("%d",&deta[i]);
			max += deta[i];
		}
			flat(0,0,0);
			printf("%d\n",sa);
	}
	return 0;
}

void flat(int joutai,int pon,int kon)
{
	if(pon + kon == max)
	{
		if(sa > abs(pon-kon)) sa = abs(pon - kon);
		return;
	}
		int kari = joutai;
		flat(joutai+1,kon+deta[kari],pon);
		flat(joutai+1,kon,pon+deta[kari]);
}

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];
		}
		//sum/2テ」ツ?ォテ、ツクツ?ァツ閉ェティツソツ妥」ツ??・ツ?、テ」ツつ津ィツソツ氾」ツ?療」ツ?淌」ツ??ッツスツ・テッツスツ・テッツスツ・
//		int ans = f(sum, 0, a, n);			//テ・ツ?催・ツクツーテ」ツ??」ツ?ィ1テ」ツ?、テ」ツ?療」ツ?凝ァツオツ静ヲツ楪愿、ツソツ敕ヲツ個?」ツ?ァテ」ツ?催」ツ?ェテ」ツ??」ツ?づ」ツつ暗」ツ?湘」ツ?ェテ」ツ??」ツ?凝」ツ?ェテ」ツ?づ」ツつ湘」ツ?凝」ツつ嘉」ツ?ャテ」ツ??
//		cout<< ans<< endl;

		//テ、ツスツ陛」ツ?凝ヲツ?ェテ」ツ?療」ツ??ヲツーツ療」ツ??
		sort(a.begin(), a.end());
		reverse(a.begin(), a.end());
		int h_sum = 0;
		int l_sum = 0;
		for(int i=0;i<n;i++){
			int temp = l_sum;
			temp += a[i];
			if(sum/2 < temp){
				h_sum = abs(h_sum-sum/2)<abs(temp-sum/2) ? h_sum : temp;
			}else{
				l_sum = temp;
			}
			if(h_sum < l_sum) h_sum = l_sum;
		}
		cout<< min(abs(2*(h_sum-1)-sum), abs(2*l_sum))<< endl;
	}
	return true;
}

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

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <complex>
#include <map>
#include <climits>
#include <sstream>
using namespace std;

#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second
#define mkp make_pair

static const double PI=6*asin(0.5);
typedef long long ll;
typedef complex<double> CP;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vint;
static const int INF=1<<24;

template <class T>
void initvv(vector<vector<T> > &v,int a,int b, const T  &t = T()){
	v.assign(a,vector<T>(b,t));
}

//v.erase(unique(v.begin(),v.end()),v.end());

vint v;
ll ans;
ll a,b;
void foo(ll c){
	if(c==v.size()){
		ans=min(ans,abs(a-b));
		return;
	}
	a+=v[c];
	foo(c+1);
	a-=v[c];
	b+=v[c];
	foo(c+1);
	b-=v[c];
}

int main(){
	int n;
	while(cin>>n,n){
		v.clear();
		ans=INF;
		a=0;
		b=0;
		rep(i,n){
			ll t;
			cin>>t;
			v.PB(t);
		}
		foo(0);
		cout<<ans<<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int n; long long a[22], ans = 1145141919810, sum = 0;
void f(int i, int s) {
	if (i == n) {
		ans = min(ans, abs((sum - s) - s));
	}
	else for (int p = 0; p <= 1; p++) {
		f(i + 1, s + a[i] * p);
	}
	return;
}
int main() {
	cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
	f(0, 0);
	cout << ans << endl;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(v[i] >= 0){
				if(t > s) s += v[i];
				else t += v[i];
			}
			else{
				if(t < s) s += v[i];
				else t += v[i];
			}
		}

		//cout<<s<<" "<<t<<endl;

		cout<<abs(s - t)<<endl;
	}
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <numeric>
#include <ctime>
#include <bitset>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>

using namespace std;

//define
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define dump(x)  cerr << #x << " = " << (x) << endl;

#define INF (INT_MAX/3)
#define PI (2*acos(0.0))
#define EPS (1e-8)
#define MOD (1000000007LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;

#define vv(t) vector< vector< T > >
#define pb push_back
#define pu push
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define EQ(a,b) (abs((a)-(b))<EPS)

int n;
int a[21];

int dfs(int diff, int i){
  if(i == n) return abs(diff);
  else{
    int s = dfs(diff + a[i], i + 1);
    int t = dfs(diff - a[i], i + 1);
    return min(s, t);
  }
}

int main(void){
  ios_base::sync_with_stdio(0);
  while(cin >> n, n){
    for(int i = 0; i < n; i++) cin >> a[i];
    cout << dfs(0, 0) << endl;
  }
  return 0; 
}