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 <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][21];
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];
			dp[0][i] = dp[1][i] = 1000000000;
		}
		int k = 0;
		rep(i, n) {
			dp[k][i] = sum - 2*a[i];
			rep(j, i) {
				if (dp[1-k][j] > 0) {
					if (abs(dp[k][j]) > abs(dp[1-k][j]-2*a[i]))
						dp[k][j] = dp[1-k][j] - 2*a[i];
				}
				/*
				else if (dp[1-k][j] < 0) {
					if (abs(dp[k][j]) > abs(dp[1-k][j]+2*a[i]))
						dp[k][j] = dp[1-k][j] + 2*a[i];
				}
				*/
				if (abs(dp[k][j]) > abs(dp[1-k][j])) {
					dp[k][j] = dp[1-k][j];
				}
			}
			k = 1 - k;
		}
		int res = 1000000000;
		rep(i, n) res = min(res, abs(dp[1-k][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 <bits/stdc++.h>

using namespace std;

#define TEMP_T template<typename T>
TEMP_T void sort(T& v){ sort(v.begin(), v.end()); }
TEMP_T void revs(T& v){ reverse(v.begin(), v.end()); }
TEMP_T void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end())); }
TEMP_T void show(T& v, char delim=' ', char end='\n'){ for(int i=0; i<v.size()-1; i++) cout << v[i] << delim; cout << v[v.size()-1] << end; }
TEMP_T T    cums(T& v){ T r; r.push_back(v[0]); for(int i=1; i<v.size(); i++) r.push_back(r[i-1] + v[i]); return r; }
TEMP_T void maxe(T& v, T m){ v = max(v, m); }
TEMP_T void mine(T& v, T m){ v = min(v, m); }

static inline int in(){ int x; scanf("%d", &x); return x; }

struct Solver {
  int mini;
  vector<int> vs;
  Solver(vector<int> vs) : vs(vs), mini(INT_MAX) {}
  void solve(int it=0, int a=0, int b=0){
    if (it == vs.size()){
      mine(mini, abs(a - b));
      return;
    }
    solve(it + 1, a + vs[it], b);
    solve(it + 1, a, b + vs[it]);
  }
  int get_min(){ return mini; }
};
int main()
{
  int n;
  while (n = in()){
    vector<int> vs;
    for (int i = 0; i < n; i++){
      vs.push_back(in());
    }
    Solver solver(vs);
    solver.solve();
    printf("%d\n", solver.get_min());
  }

  return 0;
}

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

using namespace std;

vector<int> ans;
int main(){
	while(1){
		int n;
		cin >> n;
		
		if(n == 0) break;
		
		int a[20];
		for(int i = 0;i < n;i++){
			cin >> a[i];
		}
		
		int tans = 1e9;
		for(int i = 0;i < (1 << n);i++){
			int A=0,B=0;
			for(int j = n-1;j >= 0;j--){
				if((i >> j) % 2 == 0){
					A += a[j];
				}else{
					B += a[j];
				}
			}
			tans = min(tans,abs(A - B));
		}
		
		ans.push_back(tans);
	}
	
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	return 0;
}

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

using namespace std;

int n, map[20], Min;

void search( int A, int B, int count )
{
	if(count == n) {
		Min = min(Min, abs(A-B));
		return;
	}
	search(A + map[count], B, count + 1);
	search(A, B + map[count], count + 1);
}

int main( void )
{
	while(cin >> n, n) {
		for (int i = 0; i < n; ++i) cin >> map[i];
		Min = INT_MAX;
		search(0, 0, 0);
		cout << Min << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(t > s) s += v[i];
			else t += v[i];
		}

		//cout<<s<<" "<<t<<endl;

		cout<<abs(s - t)<<endl;
	}
}

C++: #include <cstdio>
#include <algorithm>
using namespace std;
#define INF (1<<25)
 
int main(){
	int n , v[20] , X[41] , Y[41] , top;
	while(scanf("%d\n",&n)&& n){
		int dp2[21] = {0};
		for(int i = 0 ; i < n ; i++) scanf("%d",&v[i]) , memo[i].clear();
		sort(v,v+n,greater<int>());
		for(int i = n-1 ; i >= 0 ; i--) dp2[i] = dp2[i+1] + v[i];
		
		int ans = INF;
		
		top = 0;
		X[top] = Y[top] = 0;
		while(~top){
			int pos = X[top] , sum = Y[top]; top--;
			if(pos==n) ans = min(ans,sum);
			else{
				if( sum >= dp2[pos] && ans <= sum-dp2[pos] ) continue; // ツ枝ツ偲ォ
				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 <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define rep(i,n) for(int i=0; i<(n); i++)
#define repc(i,s,e) for(int i=(s); i<(e); i++)
#define pb(n) push_back((n))
#define mp(n,m) make_pair((n),(m))
#define all(r) r.begin(),r.end()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
 
const int INF = 1000000;


int main(){
	int n;
	int s = 0, t = 0;
	while(cin >> n && n > 0){
		s = 0, t = 0;
		vi v(n, 0);
		rep(i, n) cin>>v[i];
		sort(all(v));
		reverse(all(v));
		rep(i, n){
			if(t > s) s += v[i];
			else t += v[i];
		}

		cout<<s<<" "<<t<<endl;

		cout<<abs(s - t)<<endl;
	}
}