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 <tr1/unordered_map>
using namespace std;

int n , v[20];

tr1::unordered_map<int,int> memo[20];

int f(int pos , int sum){
	if(abs(sum) >= 1000001) return sum;
	return pos == n ? abs(sum) : memo[pos].count(sum) ? memo[pos][sum] : memo[pos][sum] = min( f(pos+1,sum+v[pos]) , f(pos+1,sum-v[pos]) );
}

int main(){
	while(cin >> n && n){
		for(int i = 0 ; i < n ; i++) cin >> v[i] , memo[i].clear() ;
		cout << f(0,0) << endl;
	}
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int array[20];
int n;
int ans=1000000;

void dfs(int selected,int not_selected,int index) {

  if(index==n) {

    if(ans>abs(selected-not_selected)) {

      ans=abs(selected-not_selected);

    }

    return;

  }

  else {

    dfs(selected+array[index],not_selected,index+1);

    dfs(selected,not_selected+array[index],index+1);

  }

}


int main(void) {

  int idx;
  char buf[300];

  char* tp;

  int i;

  while(1) {

    scanf("%d",&n);

    if(n==0) break;

    getchar();

    fgets(buf,sizeof(buf),stdin);

    buf[strlen(buf)-1]='\0';

    tp=strtok(buf," ");
    array[0]=atoi(tp);

    idx=1;

    while((tp=strtok(NULL," "))!=NULL) {

	array[idx]=atoi(tp);
	idx++;

    }

    //for(i=0;i<idx;i++) printf("%d ",array[i]);

    dfs(0,0,0);

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

  }

  return 0;

}

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

int main(){
  int n;
  while(cin>>n,n){
    int a[20],sum=0;
    for(int i=0;i<n;i++)cin>>a[i],sum+=a[i];
    
    S mp;
    mp.insert(0);
    for(int i=0;i<n;i++)
      for(S::iterator it=mp.begin();it!=mp.end();it++)mp.insert((*it)+a[i]);
    
    int ans=1e9;
    for(S::iterator it=mp.begin();it!=mp.end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  return 0;
}

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

using namespace std;

int main(){

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

		int Asum=0;
		vector<int>A,B;
		for(int i = 0; i < n; i++){
			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;
			}
		}

		int 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 <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int a[20];

int dfs(int n, int i, int A, int B)
{
  static int diff = 1000000;

  if (i == 0) diff = 1000000;
  if (i == n) {
    diff = min(diff, abs(A - B));
  } else {
    dfs(n, i + 1, A + a[i], B);
    dfs(n, i + 1, A, B + a[i]);
  }

  return diff;
}

int main()
{
  int n;

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

    cout << dfs(n, 0, 0, 0) << 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 dp[2][1000001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0, maxi = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
			maxi = max(maxi, a[i]);
		}
		int k = 0;
		dp[k][abs(sum-2*a[0])] = 1;
		REP(i, 1, n) {
			rep(j, maxi+1) if (dp[k][abs(sum-2*j)])
				dp[1-k][abs(sum-2*(j+a[i]))] = 1;
			rep(j, maxi+1) dp[1-k][j] |= dp[k][j];
			k = 1 - k;
		}
		rep(i, maxi+1) if (dp[1-n%2][i]) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}

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

C++: #include<stdio.h>
#include<stdlib.h>
int main(){
  int n,m,k,i,j,a,b;
  int d[20];
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    int min=1000001;
    for(i=0;i<n;i++){
      scanf("%d",&d[i]);
    }
    //k=1;
    //for(i=0;i<n;i++)k*=2;
    for(i=0;i<(1<<n);i++){
      a=b=0;
      m=i;
      for(j=0;j<n;j++){
	if(m%2)a+=d[j];
	else   b+=d[j];
	m/=2;
      }
      if(min>abs(a-b)){
	min=abs(a-b);
      }
    }
    printf("%d\n",min);
  }
  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{
				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>
#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;
  }

  t[p] = 1;
  rec(p+1);
  t[p] = 2;
  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<climits>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        unsigned long long l=0;
        int mi=INT_MAX;
        while(l<(1<<n)){
            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);
            l++;
        }
        cout<<mi<<endl;
    }
    
}