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<iostream>
#include<vector>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;

static const int MAX_MEMBERS = 20;

int Members;
int FightingPower[MAX_MEMBERS];

vector<int> power;
int seek = 0;

int FightingPower[MAX_MEMBERS], n, res;
const int INF = 1000001;

void dfs(int i, int a_sum, int b_sum) {
	if (i == n) {
		res = min(abs(a_sum - b_sum), res);
	}
	else {
		dfs(i + 1, a_sum + FightingPower[i], b_sum);
		dfs(i + 1, a_sum, b_sum + FightingPower[i]);
	}
}

int main() {
	while (cin >> n && n!=0) {
		for (int i = 0; i<n; i++) {
			cin >> FightingPower[i];
		}
		res = INF;
		dfs(0, 0, 0);
		cout << res << endl;
	}
}

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

int n;
long m, sum, a[20];

//sum/2??\?????§????????????????????????????¨?
void rec(int x, long y){ //x???????????\???????????\????????????,???????????????y
	if(x==n){
		if(m<y) m=y;	
	}
	else{
		if(y+a[x]>sum/2) rec(x+1,y);
		else{
			rec(x+1,y+a[x]);
			rec(x+1,y);		
		}	
	}
	
}

int main(){
	while(1){
		scanf(" %d", &n);
		if(n==0) break;
		
		sum=0;
		for(int i=0; i<n; ++i){
			scanf(" %ld", &a[i]);
			sum+=a[i];
		}
	
		m=0;	
		rec(0, 0);
		
		long mm=sum-m;
		//printf("m=%ld, mm=%ld\n",m ,mm);
		printf("%ld\n", mm-m);
	}
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()

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

int main(void){
  int n;
  while(cin>>n,n){
    
    vector<int> vec(n);
    rep(i,n)cin>>vec[i];
    int ans;
    vector<int> f,s;
    rep(j,1<<n){
      int a,b;
      vector<int> aa,bb;;
      rep(i,n){
	if(i%2)a+=vec[i];
	else b+=vec[i];
      }
      a>b?ans=min(a-b,ans):ans=min(b-a,ans);
    }
    cout<<ans<<endl;
    
    

    
  }
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<cmath>
#define N 1000
using namespace std;
int n,C[N],tans;


int ans(int i,int A,int B)
{
  if(i == n)
    {
      tans = min(tans,abs(A-B)); 
    }
  else
    {
      ans(i+1,A+C[i],B);
      ans(i+1,A,B+C[i]);
    }

  return tans;

}


int main()
{
  while(1)
    {
      cin >> n;
      if(n == 0)break;
      for(int i=0;i<n;i++)
	{
	  cin >> C[i];
	}
       tans = INT_MAX;
      cout << ans(0,0,0) << endl;

    }



}

C++: #include <iostream>
#include <queue>
#define abs(a) (a)>0 ? (a) : (-a)

using namespace std;

int main(){
	int n;
	long all = 0, party0 = 0, party1 = 0, diff;
	priority_queue<long> pq;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			pq.push(input);
			all += input;
		}
		for (int i = 0; i < n; i++){
			if (party0 > party1){
				party1 += pq.top();
				pq.pop();
			} else{
				party0 += pq.top();
				pq.pop();
			}
		}
		diff = abs(party0 - party1);
		cout << diff << endl;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
#include<stack>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()

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

int main(void){
  int n;
  while(cin>>n,n){
    
    vector<int> vec(n);
    rep(i,n)cin>>vec[i];
    int ans=1000000;
    vector<int> f,s;
    rep(j,1<<n){
      int a=0,b=0;
      rep(i,n){
	//cout<<j<<endl;
	if((j>>i)%2)a+=vec[i];
	else b+=vec[i];
      }
      ans= min(ans,abs(a-b));
      //cout<<ans<<endl;
    }
    cout<<ans<<endl;
    
    

    
  }
}

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

using namespace std;

int main(){

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

		int ret=10000000;
		for(int s = 0; s < (1 << n); s++){
			int sum=0;
			int left=0;
			for(int i = 0; i < n; i++){
				if(1 & (s>>i)){
					sum+=a[i];
				}
				else
					left+=a[i];
			}
			ret=min(ret,abs(sum-left));
			if(ret==0){
				break;
			}
		}
		cout<<ret<<endl;

		//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 <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a,c) memset(a,c,sizeof(a))

#define DEBUG(x) cout<<"#x"<<": "<<x<<endl

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = INT_MAX/3;
const ll MOD = 1000000007;
const double EPS = 1e-14;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};

int a[30];
int n;

int dfs(int x,int b,int c){
    if(x == n) return abs(b-c);
    int res = min(dfs(x+1,b+a[x],c) , dfs(x+1,b,c+a[x]));
    return res;
}

int main(){

    while(true){
	cin >> n;
	if(n == 0) break;
	REP(i,n){
	    cin >> a[i];
	}
	cout << dfs(0,0,0) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#include<bitset>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
/*string st(int bina,int n){
  string out;
  for (int i = 0; bina>0 ; i++){
    out = (char)((bina%2)+'0')+out;
    bina = bina/2;
  }
  while(out.size()!=n)out='0'+out;
  return out;
}  */
int main(){
  int n;
  while(cin>>n,n){
    int ans=9999999;
    vector<int>in(n);
    int sum2=0;
    rep(i,n){cin>>in[i];sum2+=in[i];}
    rep(i,1<<n){
      int sum=0;
      rep(j,n)if((i>>j)&1)sum+=in[j];
      if(ans>abs(2*sum-sum2))ans=abs(sum2-2*sum);
      //cout<<i<<" "<<sum<<endl;
    }
    cout<<ans<<endl;
  }

}