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 <cmath>
#include <algorithm>	// require sort next_permutation count etc.
#include <climits>
#include <limits>
//#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
		sort (party.begin(), party.end(), greater<int>() );

		int termA = 0, termB = 0; 

		termA = party[0];

		if (n == 1 ){
			return termA;
		} // end 

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

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9

int main(){
	int n;
	int data[21];
	while(cin>>n,n){
		int ret = INT_MAX;
		rep(i,n)cin>>data[i];
		rep(i,(1<<n)){
			int a=0,b=0;
			for(int j=0; j<n; j++){
				if( i&(1<<j) ){
					a += data[j];
				}else{
					b += data[j];
				}
			}
			ret = min(ret, abs(a-b));
		}
		printf("%d\n",ret);
	}
	return 0;
}

C++: #include <stdio.h>

int min(int,int);
int getPowerDiff(int,int,int,int[20]);

int main(){
  int n,i,a[20];
  while(1){
    scanf("%d",&n);
    if(n == 0){ break; }
    for(i = 0 ; i < n ; i++){
      scanf("%d",&a[i]);
    }
    printf("%d\n",getPowerDiff(0,0,n-1,a));
  }
  return 0;
}


/*
  aテ」ツ?ィbテ」ツ?ョテヲツ慊?・ツーツ湘・ツ?、テ」ツつ津ィツソツ氾」ツ??
 */
int min(int a,int b){
  if(a < b){
    return a;
  }else{
    return b;
  }
}

/*
  aテ」ツ?ィbテ」ツ?ョテ・ツキツョテ」ツ?ョテァツオツカテ・ツッツセテ・ツ?、テ」ツつ津ィツソツ氾」ツ??
 */
int myAbs(int a,int b){
  if(a < b){
    return b - a;
  }else{
    return a - b;
  }
}

/*
  Aテ」ツ?ッテ」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツ青暗ィツィツ暗・ツ?、.
  Bテ」ツ?ッテ」ツつーテ」ツδォテ」ツδシテ」ツδ唯テ」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツ青暗ィツィツ暗・ツ?、.
  iテ」ツ?ッテ、ツサツ甘」ツ?ゥテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津・ツッツセティツアツ。テ」ツ?ィテ」ツ?療」ツ?ヲテ」ツ??」ツつ凝」ツ??
 */
int getPowerDiff(int A,int B,int i,int a[20]){
  /*
    i == -1テ」ツ?ィテ」ツ?ェテ」ツつ凝」ツ?ィテ」ツ??
    テ・ツ?ィテ」ツ?ヲテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?セテ」ツ?淌」ツ?ッBテ」ツ?ォテ・ツ按?」ツ?妥」ツ?淌」ツ?ョテ」ツ?ァ
    テ」ツ?敕」ツ?ョテ」ツ?ィテ」ツ?催」ツ?ョテヲツ按ヲテゥツ猟佚・ツ環崚」ツ?ョテ・ツキツョテ」ツ?ョテァツオツカテ・ツッツセテ・ツ?、テ」ツつ津ィツソツ氾」ツ??
   */
  if(i == -1){
    return myAbs(A,B);
  }
  
  /*
    iテァツ閉ェテァツ崢ョテ」ツ?ョテァツ卍サテゥツ個イティツ??」ツつ津」ツつーテ」ツδォテ」ツδシテ」ツδ輸テ」ツ?ォテ・ツ環?」ツ?暗」ツつ凝」ツ?凝」ツ??」ツつーテ」ツδォテ」ツδシテ」ツδ唯テ」ツ?ォテ・ツ環?」ツ?暗」ツつ凝」ツ??
   */
  return min(getPowerDiff(A+a[i],B,i-1,a),
             getPowerDiff(A,B+a[i],i-1,a));
}

C++: #include<iostream>
#include<cmath>
using namespace std;
#define INF = (1 << 30)
#define rep(i,n) for(int i = 0 ; i < (n) ; ++i)
int n,ANS,vc[20];

void dfs(int rec,int now){
  if(now == n){
    ANS = min(ANS,abs(rec));
    return;
  }
  dfs(rec+vc[now],now+1);
  dfs(rec-vc[now],now+1);
}

int main(){
  while(cin >> n , n){
    ANS = INF;
    rep(i,n) cin >> vc[i];
    dfs(0,0);
    cout << ANS << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int n;
int a[21];

int f(int i,int sum1,int sum2){
	if(i==n)return abs(sum1-sum2);
	return min(f(i+1,sum1+a[i],sum2),f(i+1,sum1,sum2+a[i]));
}

int main(){
	while(cin>>n,n){
		rep(i,n)cin>>a[i];
		cout<<f(0,0,0)<<endl;
	}
	return 0;
}

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

#define INF 1000000000
int n,a[21],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;
  }
}

C++: #include <iostream>
#include <cmath>
#define MIN(a,b) ((a>b)?(b):(a))
using namespace std;
int m=9999999;
int a[20];
	int n;
int solve(int i,int x,int y){
	if(i==n){
		m=MIN(abs(x-y),m);
		return 0;
	}
	solve(i+1,x,y+a[i]);
	solve(i+1,x+a[i],y);
	return 0;
}

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

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

C++: #include <cstdio>
#include <algorithm>
#include <tr1/unordered_set>
using namespace std;

int n , v[20] , ans = 0;

tr1::unordered_set<int> memo[20];

int X[421] , Y[421] , top;
 
int main(){
	while(scanf("%d\n",&n)&& n){
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() ;
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(memo[pos].count(sum)) continue;
			else memo[pos].insert(sum);
			if(pos==n) ans = min(ans,sum);
			else{
				X[++top] = pos+1;
				Y[top]   = sum+v[pos];
				X[++top] = pos+1;
				Y[top]   = abs(sum-v[pos]);
			}
		}
		printf("%d\n",ans);
	}
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count etc.
#include <cstdlib>	// require abs
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
//#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;
}