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];}
		if(n<4){
			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 <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



int n;
int a[25];

int main() {
    while (cin >> n) {
        if (n == 0) break;
        int sum = 0;
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        int res = 1<<29;
        for (int bit = 0; bit < (1<<n); ++bit) {
            int tmp = 0;
            for (int j = 0; j < n; ++j) if (bit & (1<<j)) tmp += a[j];
            chmin(res, abs(sum - tmp*2));
        }
        cout << res << endl;
    }
    return 0;
}

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

const int INF = 1 << 30;

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

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

    int ans = INF;
    for (int i = 0; i < (1 << n); ++i) {
      int p = 0, b = 0;
      for (int j = 0; j < n; ++j) {
	if (i & (1 << j))
	  p += a[j];
	else
	  b += a[j];
      }
      ans = min(ans, abs(p-b));
    }

    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int n;
int memo = -1;
int *sa, *sb, *sc, *a;

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

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>
#include <stdlib.h>
#include <algorithm>
#define uint unsigned int

using namespace std;

int main(int argc, char **argv)
{
	uint *a, g, p, n;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		} else if(n == 1){
			cin >> p;
			cout << p << endl;
			continue;
		}
		a = (uint *)calloc(n, sizeof(uint));
		
		for(int i = 0; i < n; i++){
			cin >> p;
			a[i] = p;
		}
		
		p = g = 0;
		sort(a, a+n);
		for(int i = n-1; i >= 0; i--){
			if(p > g){
				g += a[i];
			} else {
				p += a[i];
			}
		}
		p -= g;
		cout << ((p < 0)? -p : p) << endl; 
		
		free(a);
	}
	
	
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a,b,x,c[100];
void mutu(int y,int z,int zz){
    if(y+z==b){
        if(x>abs(y-z)) x=abs(y-z);
    }
    else{
        mutu(y+c[zz],z,zz+1);
        mutu(y,z+c[zz],zz+1);
    }
}
int main(){
    while(1){
        x=1000000;
        b=0;
    cin>>a;
    if(a==0) break;
    for(int i=0;i<a;i++){
        cin>>c[i];
        b+=c[i];
    }
    mutu(0,0,0);
    cout<<x<<endl;
    }
    return 0;
}

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

int N, n[21];

int dfs(int suma, int sumb, int k){
    int a, b;
    if(k >= N)   return abs(suma - sumb);
    a = dfs(suma+n[k], sumb, k+1);
    b = dfs(suma, sumb+n[k], k+1);
    return min(a, b);
}

int main(void){
    while(cin >> N){
        if(N==0)    break;
        for(int i=0; i<N; i++){
            cin >> n[i];
        }
        cout << dfs(0, 0, 0) << endl;
    }
    return 0;
}

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


int main(){
    while(true){
        int N;cin>>N;
        if(!N)break;
        vector<int>as(N);
        for(int i=0;i<N;++i){
            int a;cin>>as[i];
        }
        int sum=accumulate(as.begin(),as.end(),0);
        int ans=1e9;
        for(int i=0;i<(1<<N);++i){
            int asum=0;
            bitset<20>bs(i);
            for(int j=0;j<N;++j){
                if(bs[j]){
                    asum+=as[j];
                }
            }
            ans=min(ans,abs(sum-2*asum));
        }
        cout<<ans<<endl;
    }
    return 0;

}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <tr1/unordered_set>
using namespace std;

int n , v[20] , ans = 0;

tr1::unordered_set<int> memo[20];

int X[421] , Y[421] , top;
 
int main(){
	while(scanf("%d\n",&n)&& n){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		sort(v,v+n);
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if( ( sum >= dp2[pos] && ans <= sum-dp2[pos] ) || memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				X[++top] = pos+1;
				Y[top]   = sum+v[pos];
				X[++top] = pos+1;
				Y[top]   = abs(sum-v[pos]);
			}
		}
		printf("%d\n",ans);
	}
}