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 <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];}
		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)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-1)+ss)*2-sum)));
		}
		/*for(int i=0;i<1<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
			dif=min(dif,abs(s[i]*2-sum));
		}*/
		cout<<ans<<endl;
		//sort(s,s+(1<<n));
		//for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
		/*int lb=0,ub=1<<n-1,prv=0;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]==prv)break;
			if(s[mid]>=0)ub=mid;
			else lb=mid;
			prv=s[mid];
		}
		cout<<abs(s[ub])<<endl;*/
		//cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
	}
}

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

vector<int> p;
int n, a[50], sum;

/* sum : i<k テつづδ愿つづδつづδ径[i] テつづδ古つ組テつづδ敕つ催つつづδュテつづつケテつづδ古つ和 */
void cmb( int k, int sum ){
  if( k==n ){
    p.push_back( sum );
    return;
  }

  cmb( k+1,sum+a[k] );
  cmb( k+1,sum      );
}


int main(){
  int b,c,d;
  while( cin>>n && n ){
    sum=0;
    for( int i=0;i<n;i++ ){
      cin >> a[i];
      sum += a[i] ;
    }

    p.clear();
    cmb( 0,0 );
    sort( p.begin(),p.end() );

    int min=sum;
    for( unsigned int i=0;i<p.size();i++ ){
      b=p[i];
      c=sum-p[i];
      d=abs( b-c );
      if( d<min )min=d;
    }
    cout << min << endl;
  }

  return 0;
}

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(t > s) s += v[i];
			else t += v[i];
		}

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

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 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];
			if (res > abs(sum - 2 * A)) res = abs(sum - 2 * A);
			//res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<map>

using namespace std;

int n;
int *sa, *sb, *sc, *a;

int bit(int m) {
	int ans = 1;
	for (int i = 0; i < m; i++)
		ans *= 2;
	return ans;
}

int main() {
	while (1) {
		cin >> n;
		if (n == 0)
			break;
		sa = new int[bit(n)];
		sb = new int[bit(n)];
		sc = new int[bit(n)];
		a = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		for (int i = 0; i < bit(n); i++) {
			for (int j = 0; j < n; j++) {
				if (i >> j & 1)
					sa[i] += a[j];
				else
					sb[i] += a[j];
			}
			sc[i] = abs(sa[i] - sb[i]);
		}
		int min = 1000000;
		for (int i = 0; i < bit(n); i++) {
			if (min > sc[i])
				min = sc[i];
		}
		cout << min << endl;


	}


	return 0;
}

C++: #include <iostream>
#define INF (1<<21)
using namespace std;

int n,t[20],mi,a[20];

void rec(int p){
  int a1,a2;
  if(p == n){
    a1 = 0;
    a2 = 0;
    for(int i=0;i<n;i++){
      if(t[i] == 1) a1 += a[i];
      else a2 += a[i];
    }
    if(a1 - a2 >= 0 && a1 - a2 < mi) mi = a1 - a2;
    else if(a2 - a1 > 0 && a2 - a1 < mi) mi = a2 - a1;
    return;
  }

  for(int k=1;k<=2;k++){
  t[p] = k;
  rec(p+1);
  }
}

int main(){
  int i,j;
  while(1){
    cin >> n;
    if(n == 0) break;
    for(i=0;i<n;i++){
      cin >> a[i];
      t[i] = 0;
    }

    mi = INF;
    rec(0);
    cout << mi << endl;

  }
  return 0;
}

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));
	}
	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 <cstdio>
#include <algorithm>
#include <array>
#include <vector>

using namespace std;

int sum( vector<int> v ) {
  int sum = 0;
  for ( const int val : v ) {
	sum += val;
  }
  return sum;
}

int diff( int i, vector<int>& v ) {
  int sumA = 0;
  int sumB = 0;
  for ( int index = 0; index < v.size(); index++ ) {
	if ( i % 2 == 0 ) {
	  sumA += v[ index ];
	} else {
	  sumB += v[ index ];
	}
	i /= 2;
  }
  return abs( sumA - sumB );
}

int main() {
  
  while( true ){
	int n;
	cin >> n;
	if ( n == 0 ) break;
	vector<int> v( n );
	for ( int i = 0; i < n; i++ ) {
	  cin >> v[ i ];
	}
	int ans = sum( v );
	for ( int i = 0; i < (1 << n); i++ ) {
	  ans = min( ans, diff( i, v ) );
	}
	cout << ans << endl;
  }  
}