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 <functional>

using namespace std;

int main( void )
{
	int n;
	int teamA, teamB, map[20];
	while(cin >> n, n) {
		int sum = 0, ave;
		for (int i = 0; i < n; ++i) { cin >> map[i]; sum += map[i]; }
		ave = sum / 2;
		sort(map, map+n, greater<int>());
		teamA = teamB = 0;
		for (int i = 0; i < n; ++i) {
			if(teamA + map[i] == ave)
				{ teamA += map[i]; continue; }
			else if(teamB + map[i] == ave)
				{ teamB += map[i]; continue; }
			if(teamA > teamB) teamB += map[i];
			else				teamA += map[i];
		}
		cout << abs(teamA - teamB) << endl;
	}
	return 0;
}

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){
		int a=0,b=0;
		vector<int> s(n);
	
	for(int i=0;i<n;i++)
		cin>>s[i];
	
	sort(s.begin(),s.end(),greater<int>());
	
	
		for(int i=0;i<n;i++){
			if(a>b)
				b+=s[i];
			else
				a+=s[i];
		}
		cout<<abs(a-b)<<endl;
	}
}

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

using namespace std;

#define PONTA true
#define GONTA false

int n;
int getMin(bool* cmb, bool party, int i, int* pows);
int getDiff(bool* cmb, int* pows);

int main(){
	while(cin >> n && n){
		int pows[n];
		bool cmb[n];
		for(int i = 0; i < n; i++) cin >> pows[i];
	
		cout << min( getMin(cmb, PONTA, 0, pows), getMin(cmb, GONTA, 0, pows) ) << endl;
	}
	return 0;
}

int getMin(bool* cmb, bool party, int i, int* pows){
	cmb[i] = party;
	return (i == n-1)? getDiff(cmb, pows):
		min( getMin(cmb, PONTA, i+1, pows), getMin(cmb, GONTA, i+1, pows) );
}

int getDiff(bool* cmb, int* pows){
	int p = 0, g = 0;
	for(int i = 0; i < n; i++){
		if(cmb[i] == PONTA) p += pows[i];
		else g += pows[i];
	}
	return abs(p-g);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <cstring>
#include <cmath>

using namespace std;


int main(int argc, char *argv[]) {
  int n;
  vector<int> member;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      int tmp;
      cin >> tmp;
      member.push_back(tmp);
    }
    sort(member.begin(), member.end(), greater<int>());

    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      if (x >= y)
        y += member[i];
      else
        x += member[i];
    }
    cout << abs(x - y) << endl;
  }
  return 0;
}

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

using namespace std;

int main(){
  int in;
  while(cin >> in && in){
    int data[in];
    for(int i = 0; i < in; i++) cin >> data[i];
    sort(data,data+in);
    reverse(data,data+in);
    int sum[2] = {0,0};
    for(int i = 0; i < in; i++){
      if(sum[0] > sum[1]) sum[1] += data[i];
      else sum[0] += data[i];
    }
    cout << abs(sum[0]-sum[1]) << endl;
  }
  return 0;
}

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

using namespace std;


int a[100];
int minimum = 100000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

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

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
#define mk make_pair
using namespace std;
int a[20],n;
unordered_map<pair<pair<int,int>,int>,int >m;
int dfs(int d,int x,int y){
  if(d==n){if(y<x)swap(x,y);return y-x;}
  return min(dfs(d+1,x+a[d],y),dfs(d+1,x,y+a[d]));
}
int main(){
  while(cin>>n,n){
    for(int i=0;i<n;i++)cin>>a[i];
    cout<<dfs(0,0,0)<<endl;m.clear();
  }
}