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 <vector>
#include <set>
#include <algorithm>

using namespace std;

set<int> party;

void DFS(int depth, int sum, vector<int>& a)
{
	if (depth == a.size())
	{
		party.insert(sum);
		return;
	}
	DFS(depth + 1, sum, a);
	DFS(depth + 1, sum + a[depth], a);
}

void solve()
{
	int n;
	while (cin >> n, n)
	{
		party.clear();
		int sum = 0;
		vector<int> a(n);
		for (int i = 0; i < n; ++i)
		{
			cin >> a[i];
			sum += a[i];
		}
		DFS(0, 0, a);
		int minV = 1e9;
		set<int>::iterator it = party.begin();
		while (it != party.end())
		{
			minV = min(minV, abs(sum - *it * 2));
			++it;
		}
		cout << minV << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

int n, sum, ans;
int t[22];
bool used[22];

void solve(int idx, int t_sum){
  if(idx == n){
    ans = min(ans, abs(t_sum - (sum - t_sum)));
    return;
  }

  solve(idx + 1, t_sum);
  solve(idx + 1, t_sum + t[idx]);
}

int main(){
  while(cin >> n, n){
    sum = 0;
    for(int i = 0; i < n; i++){
      cin >> t[i];
      sum += t[i];
    }

    ans = INT_MAX;
    memset(used, 0, sizeof(used));
    solve(0, 0);

    cout << ans << endl;
  }
}

C++: #include<iostream>

using namespace std;

int main(){

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

    int data[in];
    int sum[2] = {0,0};
    int n[in];

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

    for(int i = in-1; i >= 0; i--){
      if(sum[0] > sum[1]) sum[1]+=n[i];
      else sum[0]+=n[i];
    }

    sum[0] -= sum[1];
    if(sum[0] < 0) sum[0]*=-1;

    cout << sum[0] << endl;
  }
  return 0;
}

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

using namespace std;

int arr[20];
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    while(true) {
        int N;
        cin >> N;
        if(!N) break;

        for(int i = 0; i < N; ++i) {
            cin >> arr[i];
        }
        int ans = 100000000;
        for(int pat = 0; pat < (1<<N); ++pat) {
            int suma = 0, sumb = 0;
            for(int i = 0; i < N; ++i) {
                if(pat & (1<<i)) suma += arr[i];
                else sumb += arr[i];
            }
            ans = min(ans, abs(suma-sumb));
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int n;
int mn=2000000000;
int a[20];

void dfs(int battle , int depth){
	if(depth==n){
		mn=min( mn , abs(battle));
	} else {
		dfs(battle+a[depth] , depth+1);
		dfs(battle-a[depth] , depth+1);
	}
}


int main(){
	while(cin>>n , n!=0){
		for(int i=0;i<n;++i){
			cin >> a[i];
		}
		dfs(0 , 0);
		cout << mn << endl;
	}
	return 0; 
}

C++: #include<iostream>
#include<math.h>

using namespace std;

int n,mem[100],sum,ans;

void search(int max,int total,int now)
{
	if(now>max)
		return ;
	
	total+=mem[now];
	search(max,total,now+1);
	total-=mem[now];
	search(max,total,now+1);
	
	if(sqrt((total*2-sum)*(total*2-sum))<ans)
		ans=sqrt((total*2-sum)*(total*2-sum));
	
	return ;
}

int main()
{
	int i,j;
	
	for(;;)
	{
		cin >> n;
		if(n==0)
			break;
		
		sum=0;
		for(i=0;i<n;i++)
		{
			cin >> mem[i];
			sum+=mem[i];
		}
		ans=sum;
		
		search(n-1,0,0);
		
		cout << ans << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	while(cin>>n,n!=0){
	vector<int> a(n);
	
	for(int i=0;i<n;i++)
		cin>>a[i];
	
	sort(a.begin(),a.end(),greater<int>());
	
	
	for(int i=0;i<n-1;i++){
		a[0]=a[0]-a[1];
		a[1]=-1000000;
		sort(a.begin(),a.end(),greater<int>());
	}
	cout<<a[0]<<endl;
	}
}

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){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		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( ans <= abs(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);
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main(){
	int n;
	priority_queue<int> que;
	while(cin>>n,n){
		int a=0,b=0,x;
		for(int i=0;i<n;i++){
			cin>>x;
			que.push(x);
		
		}
		while(!que.empty()){
		if(a>b)
		b+=que.top();
		else
		a+=que.top();
		que.pop();
		}
		cout<<abs(a-b)<<endl;
	}
	return 0;
}

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

int n;
int a[20];

int main() {
	while (cin >> n, n) {
		for (int i = 0; i < n; i++) cin >> a[i];
		
		int ssum = 0;
		for (int i = 0; i < n; i++) ssum += a[i];
		
		int ans = 1145141919;
		for (int i = 0; i < (1 << n); i++) {
			int x = 0;
			for (int j = 0; j < n; j++) {
				if ((i >> j) % 2) {
					x += a[j];
				}
			}
			ans = min(ans, abs(ssum - 2 * x));
		}
		cout << ans << endl;
	}
	return 0;
}