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 <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

const int INF = 1 << 30;
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

typedef unsigned long long ull;
typedef std::pair<int,int> P;

int N;
int a[20];

int main(){
    while(std::cin >> N, N){
        REP(i, N){
            std::cin >> a[i];
        }

        int min_diff = INF;
        REP(i, 1<<N){
            int a_sum = 0, b_sum = 0;
            REP(j, N){
                if(i >> j & 1){
                    a_sum += a[j];
                }else{
                    b_sum += a[j];
                }
            }
            min_diff = std::min(min_diff, std::abs(a_sum-b_sum));
        }
    
        std::cout << min_diff << std::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.insert(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 <cstdio>
#include <string.h>
#include <iostream>

using namespace std;

int abs(int m){
	if(m > 0)
		return m;
	else
		return -m;
}

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 <iostream>
#include <vector>
#include <climits>
#include <cmath>
//#include <fstream>	// require freopen

using namespace std;

int main()
{
//	cut here before submit 
//	freopen ("testcase.splitup", "r", stdin );
	int n;

	while (cin >> n && n ){
		vector <int> party(n);
		for (int i = 0; i < n; ++i ){
			cin >> party[i];
		} // end for

		int termA = 0, termB = 0; 
		int res = INT_MAX;

		termA = party[0];

		if (n == 1 ){
			return termA;
		}else if (n == 2 ){
			termB = party[1];
			res = abs (termA - termB );
			cout << res << endl;
		}else{ // end if

			for (int i = 0; i < (1 << n); ++i ){
				termA = 0, termB = 0;
				for (int j = 0; j < n; ++j ){
					if (i & (1 << j ) ){
						termA += party[j];
					}else{
						termB += party[j];
					} // end if
				} // end for
				int diff = abs (termA - termB );
				res = min (res, diff );
			} // end for
			cout << res << endl;
		} // end if
	} // end loop
		
	return 0;
}

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

int main(){
  int n;
  while(cin >> n, n){
    vector<int> V(n);
    for(int i = 0 ; i < n ; i++) cin >> V[i];
    
    int ans = 1e9;
    for(int i = 0 ; i < (1<<n) ; i++){
      int A = 0, B = 0;
      for(int j = 0 ; j < n ; j++){
	if((i>>j) & 1) A += V[j];
	else B += V[j];
      }
      ans = min(ans, abs(A-B));
    }
    cout << ans << endl;
  }  
  return 0;
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int solve(vector<int>& a)
{
    int n = a.size();
    set<int> si;
    si.insert(0);

    for(int i=0; i<n; ++i){
        set<int> si1;
        while(!si.empty()){
            int tmp = *(si.begin());
            si.erase(si.begin());
            si1.insert(tmp + a[i]);
            si1.insert(abs(tmp - a[i]));
        }
        si = si1;
    }
    return *(si.begin());
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<int> a(n);
        for(int i=0; i<n; ++i)
            cin >> a[i];
        cout << solve(a) << endl;
    }
}

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 bUsod[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; 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);
	}


}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

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

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

int main()
{
    ios::sync_with_stdio(false);

    int N;
    while (cin >> N, N) {
        vector<int> A(N);
        rep(i, N) cin >> A[i];
        ll ans = INFLL;

        for (int g=0; g<(1<<N); g++) {
            ll sum1 = 0, sum2 = 0;
            for (int i=0; i<N; i++) {
                (g>>i & 1 ? sum1 : sum2) += A[i];
            }
            ans = min(ans, abs(sum1 - sum2));
        }

        cout << ans << endl;
    }
}

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

int dfs(int n,int x[20],int count,int N,int sum){
  if(n==N){
    return abs(sum-(count-sum));
  }else{
    return min(dfs(n,x,count,N+1,sum+x[N]),dfs(n,x,count,N+1,sum));
  }
}

int main(){
  int n;
  int x[20];
  while(1){
    cin >> n;
    if(n==0){
      return 0;
    }
    int count=0;
    for(int i=0;i<n;i++){
      cin >> x[i];
      count += x[i];
    }
    cout << dfs(n,x,count,0,0) <<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;
}