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>
#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]=0;
		sort(a.begin(),a.end(),greater<int>());
	}
	cout<<a[0]<<endl;
	}
}

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],s1[1<<10];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int n2=n/2;
		fill(s1,s1+(1<<n2),0);
		int sum=0,dif=1e+9;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		if(n<11){
			fill(s,s+(1<<n),0);
			for(int i=0;i<1<<n;i++){
				int sss=0;
				for(int j=0;j<n;j++)if((i/(1<<j))%2)sss+=p[j];
				dif=min(dif,abs(sss*2-sum));
			}
			cout<<dif<<endl;
		}
		else{
		for(int i=0;i<1<<n2;i++){
			for(int j=0;j<n2;j++)if(i>>j&1)s1[i]+=p[j];
		}
		sort(s1,s1+(1<<n2));
		int ans=1e+9;
		for(int i=0;i<1<<(n-n2);i++){
			int ss=0;
			
			for(int j=0;j<n-n2;j++)if(i>>j&1)ss+=p[j+n2];
			ans=min(ans,min(abs((*lower_bound(s1,s1+(1<<n2),(sum)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)-1)+ss)*2-sum)));
			ans=min(ans,abs((*(lower_bound(s1,s1+(1<<n2),(sum)/2-ss)+1)+ss)*2-sum));
		}
		cout<<ans<<endl;
		}
	}
}

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

int abs(int a){return a < 0 ? -a : a;}
int main(){
	int n,a,b;
	int p[20];
	int mi = 0;
	while(cin >> n && n){
		mi = (1<<21);
		for(int i=0;i<n;i++)cin >> p[i];
		
		for(int i=0;i<(1<<n);i++){
			a = b = 0;
			for(int j=0;j<n;j++){
				if( (1<<j) & i)a+=p[j];
				else b+= p[j];
			}
			mi = min( mi,abs(a-b) );
		}
		cout << mi << endl;
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <complex>

#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define rtr(c, i) for (typeof((c).rbegin()) i = (c).rbegin(); i != (c).rend(); --i)
#define contains(c, x) ((c).find(x) != (c).end())
#define Contains(c, x) (find(all(c), x) != (c).end())
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, (n))
#define To_String to_string< char,std::char_traits<char>,std::allocator<char> >

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int dp[2][20000001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		memset(dp, 0, sizeof(dp));
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int k = 0;
		dp[k][a[0]] = 1;
		REP(i, 1, n) {
			dp[1-k][a[i]] = 1;
			rep(j, sum+1) if (dp[k][j]) {
				dp[1-k][j+a[i]] = 1;
				dp[1-k][j] = 1;
			}
			k = 1 - k;
		}
		int res = 1000000000;
		rep(i, sum+1) if (dp[k][i])
			res = min(res, abs(sum - 2*i));
		cout << res << endl;
	}
	return 0;
}

#if 0
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
		}
		int res = 1000000000;
		rep(i, (1<<n)) {
			int A = 0;
			rep(j, n) if (i & (1 << j)) A += a[j];
			res = min(res, abs(A - (sum - A)));
		}
		cout << res << endl;
	}
	return 0;
}
#endif

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

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;
int a[128];

int dfs(int p, int g, int c){
  if(n == c) return abs(p-g);
  return min(dfs(p+a[c], g, c+1), dfs(p, g+a[c], c+1));
}

int main(){
  while(scanf("%d", &n) && n){
    rep(i, n) scanf("%d", a+i);
    printf("%d\n", dfs(0, 0, 0));
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef set<int> S;
int n,a[20];
int dfs(int idx,int A,int B){
  if(idx==n) return abs(A-B);
  return min(dfs(idx+1,A+a[idx],B),dfs(idx+1,A,B+a[idx]));
}

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

C++: #include<iostream>
#include<set>
#include<string>
#include<algorithm>
using namespace std;
int main() {
	int n;
	while (cin >> n&&n != 0) {
		int a[20];
		for (int i = 0; i < n; i++) 
			cin >> a[i];
		sort(a, a + n);
		reverse(a, a + n);
		int A = 0, B = 0;
		for (int i = 0; i < n; i++) {
			if (A <= B)
				A += a[i];
			else B += a[i];
		}
		int b = A - B;
		if (b < 0) b = 0 - b;
		cout << b << endl;
	}
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
#include<unordered_map>
using namespace std;
typedef long long ll;
ll c[20], n;
ll solve(int i, ll a, ll b) {
	if (i == n) {
		ll d = a - b;
		if (d < 0) d = 0 - d;
		return d;
	}
	ll sum = 900000000000000000;
	sum = min(sum, solve(i + 1, a + c[i], b));
	sum = min(sum, solve(i + 1, a, b + c[i]));
	return sum;
}
int main() {
	while (cin >> n&&n != 0) {
		for (int i = 0; i < n; i++)
			cin >> c[i];
		cout << solve(0, 0, 0) << endl;
	}
}