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++: 
	int n;
	while(cin>>n&&n!=0){
		int a[20];
		int sum=0;
		for(int i=0; i < n; i++){
			cin >> a[i];
			sum+=a[i];
		}
		sort(a,a+n);

		int Asum=0;
		vector<int>A,B;
		for(int i = 0; i < n; i++){
			if(Asum < sum){
				A.push_back(a[i]);
				Asum+=a[i];
				sum-=a[i];
			}
			else{
				for(int j = i; j < n; j++){
					B.push_back(a[j]);
				}
				break;
			}
		}

		int minSum = abs(Asum-sum);
		for(int i = 0; i < A.size(); i++){
			if(Asum <= sum)
				break;
			Asum-=a[i];
			sum+=a[i];
			minSum = min(minSum,abs(Asum-sum));
		}
		cout << minSum << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<climits>
using namespace std;
int mn,box[30],n;
void func(int now,int a,int b){
	if(now==n){
		int abs;
		if(a-b>=0)
		abs=a-b;
		else
		abs=b-a;
		if(mn>abs)
			mn=abs;
	}
	else{
		func(now+1,a+box[now],b);
		func(now+1,a,b+box[now]);
	}
}
int main(){
	int x;
	while(cin>>n,n){
		mn=INT_MAX;
		for(int i=0;i<n;i++){
			cin>>x;
			box[i]=x;
		}
		func(0,0,0);
		
		
		cout<<mn<<endl;
	}
	return 0;
}

C++: # include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
template<typename T>vector<T> make_v(size_t a){ return vector<T>(a); }
template<typename T,typename... Ts>auto make_v(size_t a, Ts... ts){ return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...)); }
template<typename T,typename V>typename enable_if<is_class<T>::value==0>::type fill_v(T &t, const V &v){ t = v; }
template<typename T,typename V>typename enable_if<is_class<T>::value!=0>::type fill_v(T &t, const V &v){ for(auto &e : t) fill_v(e, v); }
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
LL n,a[30],dp[1<<21],sum,ans;
int main(){
   while(cin>>n&&n!=0){ 
       ans=INF<LL>();
       sum=0;
    REP(i,n){
       cin>>a[i];
       sum+=a[i];
    }
    REP(i,1<<n){
        dp[i]=0;
        LL cnt=0;
        REP(j,n){
            if(i&(1<<j))
            dp[i]+=a[j],cnt++;
        }
        if(cnt>0&&cnt<n)
        ans=min(ans,abs(sum-dp[i]*2));
    }
    cout<<ans<<endl;
   }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <ctype.h>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> dP;

const int INF = 1000000000;
const double EPS = 1e-10;

int main(){
	while(1){
		int n;
		cin >> n;
		if(!n) break;
		vector<int> v(n);
		int sum = 0;
		rep(i, n){
			cin >> v[i];
			sum += v[i];
		}
		vector<int> subset;
		subset.push_back(0);
		rep(i, n){
			int prevsize = subset.size();
			rep(j, prevsize){
				subset.push_back(v[i]+subset[j]);				
			}
		}

		sort(all(subset));

		int m = sum;
		rep(i, subset.size()){
			int d = sum - 2*subset[i];
			if(d < 0) break;
			m = min(m, d); 
		}
		cout << m << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<climits>
using namespace std;
int mn,box[30],n;
void func(int now,int a,int b){
	if(now==n){
		if(mn>abs(a-b))
			mn=abs(a-b);
	}
	else{
		func(now+1,a+box[now],b);
		func(now+1,a,b+box[now]);
	}
}
int main(){
	int x;
	while(cin>>n,n){
		mn=INT_MAX;
		for(int i=0;i<n;i++){
			cin>>x;
			box[i]=x;
		}
		func(0,0,0);
		
		
		cout<<mn<<endl;
	}
	return 0;
}

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

using namespace std;

vector<int> Vec;
int n;
int Ans = 1e+9;

void DFS(int a, int b, int depth)
{
	if(depth == n)
	{
		Ans = min(Ans, abs(a - b));
	}
	else
	{
		DFS(a + Vec[depth], b, depth + 1);
		DFS(a, b + Vec[depth], depth + 1);
	}
}

void solve()
{
	while(cin >> n, n)
	{
		Vec.resize(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> Vec[i];
		}
		sort(Vec.begin(), Vec.end(), greater<int>());
		DFS(0, 0, 0);
		cout << Ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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][1000001];
int main()
{
	ios_base::sync_with_stdio(0);
	int n, a[21];
	while (cin >> n, n) {
		int sum = 0, maxi = 0;
		rep(i, n) {
			cin >> a[i];
			sum += a[i];
			maxi = max(maxi, a[i]);
		}
		int k = 0;
		dp[k][abs(sum-2*a[0])] = 1;
		REP(i, 1, n) {
			rep(j, maxi+1) if (dp[k][abs(sum-2*j)])
				dp[1-k][abs(sum-2*(j+a[i]))] = 1;
			k = 1 - k;
		}
		rep(i, maxi+1) if (dp[1-n%2][i]) {
			cout << i << endl;
			break;
		}
	}
	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