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 <vector>
#include <algorithm>
#include <functional>
using namespace std;

int main() {
  int n;
  while(cin >>n, n) {
    vector<int> v;
    int x;
    for(int i=0; i<n; i++) {
      cin >> x;
      v.push_back(x);
    }
    sort(v.begin(),v.end(), greater<int>());

    int a=0, b=0;
    for(int i=0; i<v.size(); i++) {
      if (a>b)
        b += v[i];
      else
        a += v[i];
    }
    cout << abs(b-a) << endl;
  }
}

C++: #include <limits>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n; cin >> n && n;) {
		vector<int> powers(n);
		for(auto &e : powers) cin >> e;

		const int total = accumulate(powers.begin(), powers.end(), 0);
		unordered_set<int> s{0};
		for(const auto &p : powers) {
			unordered_set<int> tmp(s);
			for(const auto &sum : tmp) {
				s.emplace(sum + p);
			}
		}

		int ans = numeric_limits<int>::max();
		for(const auto &sum : s) {
			ans = min(ans, abs(sum - (total - sum)));
		}

		cout << ans << endl;
	}

	return EXIT_SUCCESS;
}

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++)cout<<s[i]*2-sum<<" ";
		int lb=0,ub=1<<n-1;
		while(ub-lb>1){
			int mid=(lb+ub)/2;
			if(s[mid]>=sum/2)ub=mid;
			else lb=mid;
		}
		cout<<abs(s[ub]*2-sum)<<endl;
	}
}

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

int main(void){
	int n;
	while(cin>>n,n){
		int a[20]={},b[20]={};
		int sum = 0;
		for(int i=0;i<n;++i){
			cin>>a[i];
			sum+=a[i];
		}
		
		int d = sum;
		
		bool f;
		do{
			f = false;
			for(int i=0;i<n;++i){
				for(int j=0;j<n;++j){
					if(i==j) continue;
					if(abs(sum/2-(d-a[i]+b[j]))<abs(sum/2-d)){
						f=true;
						d-=a[i]-b[j];
						int tmp=a[i];
						a[i]=b[j];
						b[j]=tmp;
					}
				}
			}
		}while(f);
		cout<<abs(sum-d-d)<<endl;
	}
	return 0;
}

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

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount
           
#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T& a, const T& b) { a = max(a, b); }
template <class T> void min_swap(T& a, const T& b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int main()
{
    int n;
    while (scanf("%d", &n), n)
    {
	int a[32];
	rep (i, n)
	    scanf("%d", a + i);

	int res = 1 << 26;
	for (int i = 0; i < 1 << n; ++i)
	{
	    int t = 0;
	    for (int j = 0; j < n; ++j)
	    {
		if (i >> j & 1)
		    t += a[j];
		else
		    t -= a[j];
	    }
	    min_swap(res, abs(t));
	}
	printf("%d\n", res);
    }
}

C++: #include<iostream>
using namespace std;
int n,pt[20],dif;
int abs(int a){
    if(a<0)a*=-1;
    return a;
}
void dfs(int atnum,int a,int b){
    if(atnum==n){
        dif=min(dif,abs(a-b));
        return;
    }
    dfs(atnum+1,a+pt[atnum],b);
    dfs(atnum+1,a,b+pt[atnum]);
}
int main(){
    while(cin>>n,n){
        dif=100000000;
        for(int i=0;i<n;i++)cin>>pt[i];
        dfs(0,0,0);
        cout<<dif<<endl;
    }
}

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

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

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

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n;
    while(cin >> n, n){
        vi v(n);
        int sum = 0, ans = inf;
        rep(i, n){ cin >> v[i]; sum += v[i]; }
        rep(i, 1<<n){
            int suma = 0, sumb = 0;
            rep(j, n){
                if((i>>j)%2) suma += v[j];
                else sumb += v[j];
            }
            ans = min(ans, abs(suma-sumb));
        }
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

bool flg(int x, int y) {
	if (x&(1 << y))return true;
	else return false;
}

int main() {
	int n;
	while (cin >> n, n) {
		int p[20];
		int sum = 0;
		for (int i = 0;i < n;i++) {
			cin >> p[i];
			sum += p[i];
		}
		int ans = 1.8e9;
		for (int i = 0;i < (1 << n);i++) {
			int a = 0;
			for (int j = 0;j< n;j++) {
				if (flg(i, j))a += p[j];
			}
			ans = min(ans, (abs(a - (sum - a))));
		}
		cout << ans << endl;
	}
	return 0;
}

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;
	}
	return 0;
}

C++: #include <cstdio>
#include <string.h>
#include <iostream>
#include <math.h>

using namespace std;

int main(void){
	int n, i, j, x, mn, d, dp[1 << 20];
	while(1){
		scanf("%d",&n);
		if(!n) break;
		memset(dp,-1,sizeof(dp));
		dp[0] = 0;
		for(i = 0;i < n;i++){
			scanf("%d",&x);
			for(j = 0;j < 1 << n;j++)
				if(dp[j] != -1) dp[j ^ (1 << i)] = dp[j] + x;
		}
		d = ~((1 << n) - 1);
		mn = 0x3FFFFFFF;
		for(i = 0;i < 1 << n;i++)
			mn = min(mn,abs(dp[i] - dp[(~i ^ d)]));
		printf("%d\n",mn);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<numeric>
#include<algorithm>

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;
}