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 <iomanip>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <limits>

int main(){
  while(true){
    int n;
    std::cin >> n;
    if (n == 0) return EXIT_SUCCESS;
    std::vector<int> a(n);
    for (int i=0; i<n; i++){
      std::cin >> a[i];
    }
    int min = std::numeric_limits<int>::max();
    for (int i=0; i<(1<<n); i++){
      int d = 0;
      for (int j=0; j<n; j++){
        d += (((i>>j)&0x1)==0 ? a[j] : -a[j]);
      }
      if(d<0) d = -d;
      if(d<min) min = d;
    }
    std::cout << min << std::endl;
  }
}

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

using namespace std;

int main(){

	int n;
	int a[20];
	while(cin >> n && n != 0){
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		sort(a,a+n,greater<int>());
		int A,B;
		A=B=0;
		for(int i = 0; i < n; i++){
			if(A > B){
				B+=a[i];
			}
			else
				A+=a[i];
		}
		cout << abs(A-B) << endl;
	}

	return 0;
}

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

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<(o)<<" ";}
#define dumpl(o) if(DBG){cerr<<#o<<" "<<(o)<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }

signed main() {
	for (int n; cin >> n&&n;) {
		vector<int> v(n); rep(i, 0, n) { cin >> v[i]; }
		int ans = INF;
		for (int mask = 0; mask < (1 << n) / 2 + 1; mask++) {
			int a = 0, b = 0;
			rep(i, 0, n) {
				if (mask&(1 << i))a += v[i];
				else b += v[i];
			}
			chmin(ans, abs(a - b));
		}
		cout << ans << endl;
	}
	return 0;
}

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

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

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int v[20];

int main(){
  while(int n = getInt()){
    int ans = INT_MAX;

    REP(i, n) v[i] = getInt();
    REP(i, (1 << n)){
      int a[2] = { 0, 0 };

      REP(j, n){
	a[(i & (1 << j)) == 0] += v[j];
      }

      ans = min(ans, std::abs(a[0] - a[1]));
    }

    printf("%d\n", ans);
  }
  return 0;
}

C++: #include<iostream>
main(){for(int n,a[20],S,s,m,i,j,t;std::cin>>n,n;printf("%d\n",m)){for(i=S=0;i<n;S+=a[i++])std::cin>>a[i];m=1<<30;for(i=(1<<n);i-->0;t=S-s-s,t=t>0?t:-t,m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

C++: #include<iostream>
#include<algorithm>
#include<math>
using namespace std;
int main(void){
	double sum,mostnear;
	bool a[20];
	int n,kyas[20],shiftmax,sssum,ansint,i,j;
	while(1){
		scanf("%d",&n);
		if(n==0)
			break;
		for(i=0;i<n;i++){
			scanf("%d",&kyas[i]);
		}
		sort(kyas,kyas+n);
		sum=0;
		for(i=0;i<n;i++){
			sum+=kyas[i];
		}
		sum/=2;
		mostnear = 2100000000;
		shiftmax = 1<<n;
		for(i=0;i<shiftmax;i++){
			for(j=0;j<n;j++){
				a[j] = i&1<<j;
			}
			sssum=0;
			for(j=0;j<n;j++){
				sssum += a[j]*kyas[j];
			}
			mostnear = mostnear>fabs(sssum-sum) ? fabs(sssum-sum) : mostnear;
		}
		printf("%d\n",(int)(mostnear*2));
	}
	return 0;
}

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

int n,a[21],mini;

void rec(int idx,int sum1,int sum2){
  if(idx == n){
    if(mini > abs(sum1 - sum2)) mini = abs(sum1 - sum2);
    return;
  }
  rec(idx+1,sum1+a[idx],sum2); rec(idx+1,sum1,sum2+a[idx]);
}


int main(){
  while(cin >> n,n){
    mini = (2 << 28);
    for(int i=0;i<n;i++) cin >> a[i];
    rec(0,0,0);
    cout << mini << endl;
  }
}

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

using namespace std;

int main(int argc, char *argv[]) {
  int pon, gon;
  int n, tmp;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    vector<int> party;
    for (int i = 0; i < n; i++) {
      cin >> tmp;
      party.push_back(tmp);
    }
    sort(party.begin(), party.end());
    pon = gon = 0;
    for (int i = 0; i < n; i++) {
      //cout << party[n-1-i];                                                                                                                     
      if (pon >= gon)
        gon += party[n-1-i];
      else
        pon += party[n-1-i];
    }
    cout << (pon-gon < 0 ? gon-pon : pon-gon) << endl;
  }
  return 0;
}

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

using namespace std;

const int MAX = 21;
const int INF = (1<<25);

int in;
int data[MAX];
int mindif;

void dfs(int i, int sum){
  if(i==in){
    mindif = min(mindif,abs(sum));
    return;
  }
  dfs(i+1,sum+data[i]);
  dfs(i+1,sum-data[i]);
}

int main(){
  while(cin >> in && in){
    for(int i = 0; i < in; i++) cin >> data[i];
    mindif = INF;
    dfs(0,0);
    cout << mindif << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
#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){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		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(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);
	}
}

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

using namespace std;

const int inf = 1000001;

int main()
{
  int n, mn, t, a[21], mask, s, u;

  while(cin>>n && n){
    t = 0;
    mn = inf;

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

    mask = 1<<n;
    for(int i = 0; i < mask; i++){
      s = 0;
      for(int j = 0; j < n; j++){
	u = 1<<j;
	if(i&u) s += a[j];
      }  
      mn = min(mn, abs(s-(t-s)));
    }

    cout << mn << endl;
  }

  return 0;
}

C++: #include<iostream>
#include<cstdlib>
main(){for(int n,a[20],S,s,m,i,j,t;std::cin>>n,n;printf("%d\n",m)){for(i=S=0;i<n;S+=a[i++])std::cin>>a[i];m=1<<30;for(i=(1<<n);i-->0;t=abs(S-s-s),m>t&&(m=t))for(j=s=0;j<n;j++)if(i&1<<j)s+=a[j];}}

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

using namespace std;

int main(){
	for(int n;scanf("%d",&n),n;){
		int a[20];
		for(int i=0;i<n;i++)	scanf("%d",a+i);

		int ans=1<<30;
		int sum=accumulate(a,a+n,0);
		for(int i=0;i<(1<<n);i++){
			int psum=0;
			for(int j=0;j<n;j++){
				if(i&(1<<j))	psum+=a[j];
			}
			ans=min(ans,abs(sum-2*psum));
		}

		printf("%d\n",ans);
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,ans;
int t[20];

void solve(int x,int a,int b){
  if(x==n){
    ans=min(ans,max(a,b)-min(a,b));
  }else{
    solve(x+1,a+t[x],b);
    solve(x+1,a,b+t[x]);
  }
}

int main(){
  while(1){
    cin>>n;
    if(n==0)break;
    for(int i=0;i<n;i++)cin>>t[i];
    ans=(1e9);
    solve(0,0,0);
    cout<<ans<<endl;
  }
  return 0;
}