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<queue>
using namespace std;
int main(){
	int n;
	priority_queue<int> que;
	while(cin>>n,n){
		int a=0,b=0,x;
		for(int i=0;i<n;i++){
			cin>>x;
			que.push(x);
		
		}
		while(!que.empty()){
		if(a>b)
		b+=que.top();
		else
		a+=que.top();
		que.pop();
		}
		cout<<abs(a-b)<<endl;
	}
	return 0;
}

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){
		int sum2 = 0;
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() , sum2 += v[i] ;
		sum2 /= 2;
		sum2+=10;
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if(sum > sum2 || memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				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 <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){
		int sum2 = 0;
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear() , sum2 += v[i] ;
		sum2 /= 2;
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if(sum > sum2 || memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				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>
using namespace std;

int n;
int data[20];

int rec(int p, int A, int B) {
  static int min;
  if(p == 0) min = 999999;
  if(p == n) {
    int temp = abs(A-B);
    if(temp < min) min = temp;
    return 0;
  }
  rec(p+1, A+data[p], B);
  rec(p+1, A, B+data[p]);
  return min;
}

int main() {
  while(1) {
    cin >> n;
    if(n == 0) break;
    for(int i = 0; i < n; i++) {
      cin >> data[i];
    }
    cout << rec(0, 0, 0) << endl;
  }
  return 0;
}

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

using namespace std;

int a[100];
int minimum = 1000000000, n, sum;
void dfs(int now,int i){
  if(i == n){
    if(minimum > abs(sum-now*2)){
      minimum = abs(sum-now*2);
      return;
    }
  }
  dfs(now, i+1);
  dfs(now-a[i], i+1);
}
int main(){
  while(1){
    sum = 0;
    cin >> n;
    if(n == 0) break;

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

    sort(a, a+n);

    dfs(sum, 0);

    cout << minimum << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
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);
    }
    int ans = 1<<30;
    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;
  }
}

C++: #include <iostream>
#include <queue>

using namespace std;

int main(){
	int n;
	long all = 0, party0 = 0, party1 = 0;
	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();
			}
		}
		cout << abs(party0-party1) << endl;
		cin >> n;
	}
	return 0;
}

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){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		ans = 99999999;
		top = 0;
		X[top] = 0;
		Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if( memo[pos].count(sum)) continue;
				else memo[pos].insert(sum);
				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<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#include<bitset>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
/*string st(int bina,int n){
  string out;
  for (int i = 0; bina>0 ; i++){
    out = (char)((bina%2)+'0')+out;
    bina = bina/2;
  }
  while(out.size()!=n)out='0'+out;
  return out;
}  */
int main(){
  int n;
  while(cin>>n,n){
    int ans=9999999;
    vector<int>in(n);
    int sum2=0;
    rep(i,n){cin>>in[i];sum2+=in[i];}
    rep(i,1<<n){
      int sum=0;
      rep(j,n)if((i>>j)&1)sum+=in[j];
      if(ans>abs(2*sum-sum2))ans=abs(sum2-2*sum);
      //cout<<i<<" "<<sum<<endl;
    }
    cout<<ans<<endl;
  }

}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a[100], ans = 9999999999;
void dfs(int g, int p, int now) {
	if (now == n) {
		ans = min(abs(g - p), ans);
		return;
	}
	dfs(g + a[now], p, now + 1);
	dfs(g, a[now] + p, now + 1);
	return;
}
int main() {
	while (1) {
		cin >> n;
		if (n == 0) return 0;
		ans = 9999999999;
		for (int i = 0; i < n; i++) cin >> a[i];
		dfs(0, 0, 0);
		cout << ans << endl;
	}
	return 0;
}

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

using namespace std;

int main()
{
	int pn, gn, n, a[21];
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		pn = gn = 0;
		for(int i = 0; i < n; i++){
			cin >> a[i];
		}
		sort(a, a+n);
		
		for(int i = n-1; i >= 0; i--){
			if(pn < gn){
				pn += a[i];
			} else {
				gn += a[i];
			}
		}
		
		pn -= gn;
		if(pn < 0){
			pn = -pn;
		}
		cout << pn << endl;
	}
	
	return 0;
}

C++: //54
#include<iostream>
#include<cstdlib>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int ans=1<<30;
    int a[20];
    for(int i=0;i<n;i++){
      cin>>a[i];
    }
    for(int i=0;i<1<<n;i++){
      int p[2]={};
      for(int j=0;j<n;j++){
	p[!(i&1<<j)]+=a[j];
      }
      ans=min(ans,abs(p[0]-p[1]));
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

int n , v[20];

tr1::unordered_map<int,int> memo[20];

int f(int pos , int sum){
	if(sum > 10000000) return 99999999;
	return pos == n ? abs(sum) : memo[pos].count(sum) ? memo[pos][sum] : memo[pos][sum] = min( f(pos+1,sum+v[pos] ) , f(pos+1,abs(sum-v[pos])) );
}

int main(){
	while(cin >> n && n){
		for(int i = 0 ; i < n ; i++) cin >> v[i] , memo[i].clear() ;
		cout << f(0,0) << endl;
	}
}