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 <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repp(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;
typedef pair<string, int> psi;

signed main(){
    int n;
    while(cin >> n, n){
        vector<int> d(n);
        int sum = 0;
        rep(i, 0, n){
            cin >> d[i];
            sum += d[i];
        }
        int ans = 1e9;
        for(int i = 0; i < 1<<n; i++){
            int tmp = 0;
            rep(j, 0, n){
                if(i & 1<<j) tmp += d[j];
            }
            ans = min(ans, abs(sum - 2*tmp));
        }
        cout << ans << endl;
    }
}

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

int a[20], n, res;
const int INF = 1000001;

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

void dfs(int i, int a_sum, int b_sum) {
    if (i==n) {
        res = min(abs(a_sum - b_sum), res);
    } else {
        dfs(i+1, a_sum+a[i], b_sum);
        dfs(i+1, a_sum, b_sum+a[i]);
    }
}

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

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main( void )
{
	int n;
	while ( cin >> n && n )
	{
		int a = 0;
		int b = 0;

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

		sort( new_members.rbegin(), new_members.rend() );

		for ( vector <int>::iterator it = new_members.begin(); it != new_members.end(); it++ )
		{
			if ( a <= b )
				a += *it;
			else
				b += *it;
		}

		if ( a > b )
			cout << a - b << endl;
		else
			cout << b - a << endl;

	}

	return 0;
}

C++: #include <iostream>
#include <queue>
#define abs(a) ((a)>0 ? (a) : -(a))

using namespace std;

long diffmin,n,p[20];
void dfs(int layer, long a, long b){
	if (layer < n){
		dfs(layer + 1, a + p[layer], b);
		dfs(layer + 1, a, b + p[layer]);
	} else {
		if (diffmin > abs(a - b)){
			diffmin = abs(a - b);
		}
		return;
	}
}
int main(){
	long all = 0, party0 = 0, party1 = 0;
	cin >> n;
	while (n){
		for (int i = 0; i < n; i++){
			long input;
			cin >> input;
			p[i] = input;
			all += input;
		}
		diffmin = all;
		dfs(0, 0, 0);
		cout << diffmin << endl;
		cin >> n;
		party0 = party1 = 0;
		all = 0;
	}
	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<iostream>
#include<stdlib.h>
#include<limits.h>
int main()
{
	int n,x[20],i,j,a,b,r,t;
	struct S{int a,b;};
	static S s[2097152];
	while(std::cin>>n,n)
	{
		for(a=b=i=0;i<n;++i)std::cin>>x[i];
		s[0].a=s[0].b=0;
		for(i=0;i<n;++i)for(j=(1<<i)-1;j<(1<<(i+1))-1;++j)
		{
			s[j*2+1]=s[j],s[j*2+1].a+=x[i];
			s[j*2+2]=s[j],s[j*2+2].b+=x[i];
		}
		for(r=INT_MAX,i=(1<<n)-1;i<(1<<(n+1))-1;++i)
			t=abs(s[i].a-s[i].b),r=(r<t?r:t);
		std::cout<<r<<'\n';
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int n;
int dfs(int i, vector<int> a, int sum, int max){
  if(i==n) return abs(max-sum*2);
  if((sum+a[i])*2>max) return dfs(i+1,a,sum,max);
  return min( dfs(i+1,a,sum+a[i],max), dfs(i+1,a,sum,max));
}
int main(void){
  while(cin>>n){
    if(n==0) break;
    vector<int> a(n);
    REP(i,n) cin>>a[i];
    int s = accumulate(a.begin(),a.end(),0);
    cout<<dfs(0,a,0,s)<<endl;
  }
  return 0;
}

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

int n, ans;
int t[22];

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

    ans = INT_MAX;

    for(int i = 0; i < (1 << n); i++){
      int sumA = 0, sumB = 0;

      for(int j = 0; j < n; j++){
        if(i & (1 << j)){
          sumA += t[j];
        }
        else{
          sumB += t[j];
        }
      }

      ans = min(ans, abs(sumA - sumB));
    }

    cout << ans << endl;
  }
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int list[20];
int func(int n,int a,int b){
	if(n == -1){
		return (a>b)?(a-b):(b-a);
	}
	return min(func(n-1,a+list[n],b),func(n-1,a,b+list[n]));
}
int main(){
	int n;
	while(cin >> n,n){
		memset(list,0,sizeof(list));
		for(int i = 0; i < n; i++){
			scanf("%d",&list[i]);
		}
		int ans = func(n,0,0);
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include<cmath>
#include<cstdio>
#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 <cmath>
#include <algorithm>
using namespace std;

const long long int INF = 1000000000; //KÈå«¢
long long int n, ans, a[21];

void dfs(int i, long long int A, long long int B){
	if( i == n ){
		ans = min( ans , abs(A-B) );
		return;
	}
	dfs( i+1 , A+a[i] , B );
	dfs( i+1 , A , B+a[i] );
	
}

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

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 mi = INF;
void rec(int num,int A,int B){
    if(A + B == sum){
        mi = min(mi,abs(A-B));
        return ;
    }
    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;
        mi = INF;
        memset(mem,0,sizeof(mem));
        rep(i,n){
            scanf("%d",&mem[i]);
            sum += mem[i];
        }
        rec(0,0,0);
        printf("%d\n",mi);
    }
	return 0;
}