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<11){
			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 <vector>
#include <numeric>
using namespace std;

int main() {
    int n;
    while (cin >> n, n) {
        vector<int> a(n);
        for (int& x : a) cin >> x;
        int sum = accumulate(a.begin(), a.end(), 0);

        int result = 0x7ffffff;
        for (int i = 0; i < (1 << n); i++) {
            int pon = 0;
            for (int j = 0; j < n; j++) {
                if ((i >> j) & 1) {
                    pon += a[j];
                }
            }
            result = min(result, abs(pon - (sum - pon)));
        }
        cout << result << endl;
    }
    return 0;
}

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

using namespace std;

int n;
int trueans = 99999;
vector<int> data;

int dfs(int i,int asum,int bsum){
	if(i == n){
		trueans = min(trueans,abs(asum-bsum));
		return trueans;
	}
	else{
		dfs(i+1,asum+data[i],bsum);
		dfs(i+1,asum,bsum+data[i]);
	}
}

int main(){
	while(1){
		cin >> n;
		if(!n)break;
		data.resize(n);
		trueans = 99999;
		for(int i=0;i<n;++i){
			cin >> data[i];
		}
		cout << dfs(0,0,0) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<complex>
#include<math.h>
using namespace std;
int main(){
	int n,k[20];
	while(true){
		cin>>n;
		if(n==0)
			break;
		int sum=0;
		for(int i=0;i<n;i++){
			cin>>k[i];
			sum+=k[i];
			k[i]*=2;
		}
		int best=1000000;
		int m=pow(2.0,n+0.0);
		for(int i=0;i<m;i++){
			int bit=i;
			int count=0;
			for(int j=0;j<n;j++){
				if(((bit>>(j+1))&1)==1)count+=k[j];}
			int r=abs(count-sum);
			best=min(best,r);
		}
		cout<<best<<endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int abs(int a){return a < 0 ? -a : a;}
int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int mi=INT_MAX;
        for(int l=0;l<(2<<n);l++){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
        }
        cout<<mi<<endl;
    }
    
}

C++: #include<iostream>
#include<cmath>
using namespace std;
const int INF = (1 << 30);
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i)
int n,ANS,vc[20];

void dfs(int rec,int now){
  if(now == n){
    ANS = min(ANS,abs(rec));
    return;
  }
  dfs(rec+vc[now],now+1);
  dfs(rec-vc[now],now+1);
}

int main(){
  while(cin >> n , n){
    ANS = INF;
    rep(i,n) cin >> vc[i];
    dfs(0,0);
    cout << ANS << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int n,u[21];
vector <int> a,b;
int minsa = 100000;

void kakunin() {
  cout << "ateam" << endl;
  for(int i=0;i<a.size();i++) cout << a[i] <<" ";
  cout << endl<< "bteam" << endl;
  for(int i=0;i<b.size();i++) cout << b[i] << " " ;
  cout << endl<<"----------------------"<<endl;;
}

void sa() {
  //  kakunin();
  int sum = 0;  
  for(int i=0;i<a.size();i++) sum += a[i];
  for(int i=0;i<b.size();i++) sum -= b[i];
  if(sum < 0) sum *= -1;
  if(minsa > sum) minsa = sum;
}
				
				
void check(int x) {
  //  kakunin();
  int flag = 0;
  if(x == n) {
    sa();
    flag = 1;
  }
  else if(flag == 0) {
    a.push_back(u[x]);
    check(x+1);
    a.pop_back();
    
    b.push_back(u[x]);
    check(x+1);
    b.pop_back();
  }
} 

int main() {

  while(1) {
    cin >> n;
    if(n == 0) break;
    for(int i=0;i<n;i++) cin >> u[i];
    minsa = 1000000;
    check(0);
    a.clear();
    b.clear();
    cout << minsa << endl;
  }

  return 0;
}

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

using namespace std;

bool table[1000001];

int main(){
	while(true){
		int n, a, sum = 0;
		cin >> n;
		if(n == 0){ break; }
		memset(table, 0, sizeof(bool) * 1000001);
		table[0] = true;
		while(n--){
			cin >> a;
			for(int i = sum; i >= 0; --i){
				if(table[i]){ table[i + a] = true; }
			}
			sum += a;
		}
		int answer = sum / 2;
		for(; answer >= 9; --answer){
			if(table[answer]){ break; }
		}
		cout << (sum - answer * 2) << endl;
	}
	return 0;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    int n;
    cin>>n;
    if(!n)break;
    vector<int> v(n);
    REP(i,n)cin>>v[i];
    int sum = accumulate(begin(v),end(v),0);
    int mx=0;
    REP(i,(1<<n)){
      bitset<20> bs(i);
      int s=0;
      REP(j,n){
        if(bs[j]){
          s+=v[j];
          if(s <= sum/2)
            mx=max(s,mx);
        }
      }
    }
    cout<<sum - mx*2<<endl;
  }
  return 0;
}

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<11){
			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)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-1)+ss)*2-sum)));
			ans=min(ans,min(abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)+1)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-2)+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;
	}
}