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

#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) REP(i,0,n)
#define INF (1<<30)
#define INFLL (1LL<<62LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

int solve(vector<int> & A, int a, int b, int idx) {
  if (idx == A.size())
    return abs(a - b);
  return min(solve(A, a + A[idx], b, idx + 1), solve(A, a, b + A[idx], idx + 1));
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int N;
  while (cin >> N, N) {
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    cout << solve(A, 0, 0, 0) << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int table[20];
int main(){
	int a;
	while(scanf("%d",&a),a){
		int sum=0;
		for(int i=0;i<a;i++){
			scanf("%d",table+i);
			sum+=table[i];
		}
		int ret=99999999;
		for(int i=0;i<(1<<a);i++){
			int val=0;
			for(int j=0;j<a;j++){
				if(i&(1<<j))val+=table[j];
			}
			ret=min(ret,abs(sum-val-val));
		}
		printf("%d\n",ret);
	}
}

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[21];
    mp[0].insert(0);
    for(int i=0;i<n;i++)
      for(S::iterator it=mp[i].begin();it!=mp[i].end();it++)mp[i+1].insert((*it)+a[i]);
    
    int ans=1e9;
    for(S::iterator it=mp[n].begin();it!=mp[n].end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  return 0;
}

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

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

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

int main(){
  int n, s1,s2;
  int a[50];

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

    sort( a,a+n );

    s1=0;s2=0;
    for( int i=n-1;i>=0;i-- ){
      if( s1>s2 )
	s2 += a[i];
      else
	s1 += a[i];
    }
    cout << abs( s1-s2 ) << endl;

  }

  return 0;
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		vector<long long int> d(n);
		long long int a = 0;
		for (int i= 0; i < n; i++) {
			cin >> d[i];
			a += d[i];
		}
		int l = 1<<n;
		long long int MIN = a;
		for (int i = 0; i < l; i++) {
			long long int b = 0;
			for (int j = 0; j < n; j++) {
				if ((i & (1<<j)) != 0) {
					b += d[j];
				}
			}
			long long int c = abs(a-b);
			long long int dd = abs(c-b);
			MIN = min(MIN, dd);
		}
		cout << MIN << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<complex>
#include<math.h>
using namespace std;int main(){int n,k[20];while(true){cin>>n;if(n==0)break;int sum=0;for(int i=0;i<n;i++){cin>>k[i];sum+=k[i];k[i]*=2;}int best=1000000,m=pow(2.0,n+0.0);for(int i=0;i<m;i++){int bit=i;int count=0;for(int j=0;j<n;j++){if((bit>>(j+1))&1==1)count+=k[j];}best=min(best,abs(count-sum));}cout<<best<<endl;}return 0;}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)

int sum;
int a[20];

bool bit(unsigned int a, int k) {
  return ((a >> (k + 1)) & 1);
}

int diff(int m, int n) {
  return ((m - n > 0) ? m - n : n - m);
}

int pow(int a, int n) {
  int ans = 1;
  REP(i, n) {
    ans *= a;
  }
  return ans;
}

int solve(int n) {
  int repeat = pow(2, n), min = sum;
  for (unsigned int bitset = 0; bitset < repeat; bitset++) {
    int agroup = 0;
    REP(k, n) {
      if (bit(bitset, k))
        agroup += a[k];
    }

    int d = diff(agroup, sum - agroup);
    if (min > d) min = d;
  }

  return min;
}

int main() {
  unsigned int n;
  while (cin >> n and n != 0) {
    sum = 0;
    REP(i, n) {
      cin >> a[i];
      sum += a[i];
    }

    cout << solve(n) << endl;/*
    int k;
    cin >> k;
    cout << bit(n, k) << endl;*/
  }
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr)/sizeof(value), (value))
template <typename T>
void FILL_(void * ptr, size_t size, T value){
  std::fill((T*)ptr, (T*)ptr+size, value);
}
//4方向ベクトル→↑←↓
int dx[] ={1,0,-1,0};
int dy[] ={0,-1,0,1};
const int INF =1e+9;
int a[30];
int n;
int ans;
void dfs(int diff,int k){
  if(k == n){
    ans = min(ans,abs(diff));
  }else {
    dfs(diff+a[k],k+1);
    dfs(diff-a[k],k+1);
  }
}

int main()
{
  while(cin>>n,n){
    for(int i = 0;i < n;i++){
      cin >>a[i];
    }
    ans = INF;
    dfs(0,0);
    cout <<ans<<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(){
    while(scanf("%d", &n) && n){
        for(int x = 0; x < n; x++){
            scanf("%d", arr+x);
        }
        printf("%d\n", dfs(0, 0));
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20];

int main(){
	while(cin>>n){
		if(!n)return 0;
		fill(s,s+(1<<n),0);
		int sum=0;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		for(int i=0;i<1<<n;i++){
			for(int j=0;j<n;j++)if((i/(1<<j))%2)s[i]+=p[j];
		}
		sort(s,s+(1<<n));
		for(int i=0;i<1<<n;i++)s[i]=s[i]*2-sum;
		/*int lb=0,ub=1<<n-1,prv=0;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]==prv)break;
			if(s[mid]>=0)ub=mid;
			else lb=mid;
			prv=s[mid];
		}
		cout<<abs(s[ub])<<endl;*/
		cout<<min(*lower_bound(s,s+(1<<n),0),abs(*(lower_bound(s,s+(1<<n),0)-1)))<<endl;
	}
}

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

using namespace std;

int main(){

	int n;
	int a[20];
	while(cin >> n && n != 0){
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		int minNum=1000000000;
		for(int s = 0; s < (1 << n); s++){
			int A,B;
			A=B=0;
			for(int j = 0; j < n; j++){
				if(1 & (s >> j)){
					A+=a[j];
				}
				else{
					B+=a[j];
				}
			}
			minNum=min(minNum,abs(A-B));
		}
		cout << minNum << endl;
	}

	return 0;
}