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 <queue>
#define abs(a) (a)>0 ? (a) : (-a)

using namespace std;

int main(){
	int n;
	long all = 0, party0 = 0, party1 = 0, diff;
	priority_queue<long> pq;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			pq.push(input);
			all += input;
		}
		for (int i = 0; i < n; i++){
			if (party0 > party1){
				party1 += pq.top();
				pq.pop();
			} else{
				party0 += pq.top();
				pq.pop();
			}
		}
		diff = abs(party0 - party1);
		cout << diff << endl;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	return 0;
}

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

using namespace std;

int main () {


	int ab[20];
	bool used[20];
	int sum;
	int aSum;
	int bSum;
	bool aUsed[20];
	bool bUsed[20];

	while (true) {
	
		int n;
		cin >> n;
		if ( n == 0)
			break;
		sum = 0;
		aSum = 0;
		bSum = 0;
		for (int i=0; i<n; i++ ) {
			cin >> ab[i];
			used[i] = false;
			aUsed[i] = false;
			bUsed[i] = false;
			sum += ab[i];
		}

		sort ( ab, &ab[n]);

		for ( int i=n-1; i>=0; i-- ) {
			if ( aSum <= bSum ) {
				aSum += ab[i];
				aUsed[i] = true;
			} else {
				bSum += ab[i];
				bUsed[i] = true;
			}
		}
		/*
		bool stop = true;
		while (true) {
			if (abs (aSum - bSum) / 2 == 0 ) {
				break;
			}

			for ( int i=0; i<n; i++ ) {
				if ( abs(aSum - bSum)/2 >= ab[i] ) {
					if ( aUsed > bUsed ) {

			}
		}
				*/
		cout << abs(aSum - bSum)<< endl;
	}


}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 21;

int a[MAX],n,res;

int dfs(int num,int x,int y){
  if(num == n){
    if(x < y){
      res = min(res,y-x);
    }
    else{
      res = min(res,x-y);
    }
  }
  else{
    dfs(num+1,x+a[num],y);
    dfs(num+1,x,y+a[num]);
  }
}

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

  return 0;
}

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

int n, arr[20];

int dfs(int m, int d){
    if(m == n) return abs(d);
    int pat1 = dfs(m + 1, d + arr[m]);
    int pat2 = dfs(m + 1, d - arr[m]);
    return min(pat1, pat2);
}

int main(){
    scanf("%d", &n);
    for(int x = 0; x < n; x++){
        scanf("%d", arr+x);
    }
    printf("%d\n", dfs(0, 0));
    return 0;
}

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

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

int main(){
    int n;
    int a[100];
    while(cin>>n && n){
        rep(i,n)cin>>a[i];
        int ans=1<<20;
        rep(i,1<<n){
            int d=0;
            rep(j,n)if(i>>j&1)d+=a[j];else d-=a[j];
            ans=min<int>(ans,abs(d));
        }
        cout<<abs(ans)<<endl;
    }
}

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

int main() {
    int N;
    while (cin >> N, N) {
        vector<int> v(N);
        int ans = 1 << 28;
        for (auto& i : v) cin >> i;
        for (int i = 1; i < (1<<N)-1; i++) {
            int A = 0, B = 0;
            for (int j = 0; j < N; j++)
                if (i&(1<<j)) A+=v[j];
                else B+=v[j];
            ans = min(ans, abs(A-B));
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <iostream>
#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( ( sum >= dp2[pos] && ans <= 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<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<k;i++)
typedef long long ll;
typedef unsigned long long ull;
int sum;
int mem[32];
int rec(int num,int A,int B){
    if(A + B == sum){
        int mi = min(mi,abs(A-B));
        return mi;
    }
    rec(num+1,A+mem[num],B);
    rec(num+1,A,B+mem[num]);
}
int main(){
    int n;
    while(scanf("%d",&n), n){
        sum = 0;
        memset(mem,0,sizeof(mem));
        rep(i,n){
            scanf("%d",&mem[i]);
            sum += mem[i];
        }
        printf("%d\n",rec(0,0,0));
    }
	return 0;
}

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

using namespace std;

int main(){
  int i, n, x, a, b, ans;
  vector<int> data;

  while(1){
    cin >> n;
    if(n == 0) break;

    for(i=0; i<n; ++i){
      cin >> x;
      data.push_back(x);
    }

    sort(data.begin(), data.end());

    for(i=n-1, a=0, b=0; i>=0; --i){
      if(a < b) a += data[i];
      else b += data[i];
    }

    if(a >= b) ans = a-b;
    else ans = b-a;

    cout << ans << endl;
    data.clear();
  }

  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int n; long long a[22];
vector<long long>v;
void f(int i, int sum) {
	if (i == n)v.push_back(sum);
	for (int p = 0; p <= 1; p++) {
		f(i + 1, sum + a[i] * p);
	}
	return;
}
int main() {
	long long sum = 0;
	cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; }
	f(0, 0);
	long long ans = 114514191810;
	for (int i = 0; i < v.size(); i++) {
		ans = min(ans, abs((sum - v[i]) - v[i]));
	}
	cout << ans << endl;
}

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

int power[20];

int abs(int n) {
  return (n < 0) ? -n : n;
}

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

    int ans = 1<<30,tmp;
    for(int i=0; i < 1<<n; ++i) {
      tmp = 0;
      for(int j=0; j<n; ++j)
	if(i & 1<<j) tmp += power[j];
      ans = min(ans, abs(sum-tmp-tmp));
    }
    cout<<ans<<endl;
  }
}

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

vector <int> new_members;
int kotae;
int a;
int b;

int score( int a, int b )
{
	if ( a > b )
		return a - b;
	else
		return b - a;
}

void solve( int n )
{
	if ( n == new_members.size() )
	{
		kotae = min( kotae, score( a, b ) );
		return;
	}

	a += new_members[n];
	solve( n + 1 );
	a -= new_members[n];

	b += new_members[n];
	solve( n + 1 );
	b -= new_members[n];

}

int main( void )
{
	int n;
	while ( cin >> n && n )
	{
		int a = 0;
		int b = 0;

		new_members = vector <int>();
		for ( int i = 0; i < n; i++ )
		{
			int new_member;
			cin >> new_member;
			new_members.push_back( new_member );
		}

		kotae = INT_MAX;
		a = 0;
		b = 0;
		solve( 0 );

		cout << kotae << endl;

	}

	return 0;
}