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 <bits/stdc++.h>

using namespace std;

template <typename T>
using vec1 = vector<T>;

template <typename T>
T cin2var()
{
    T val;
    cin >> val;
    return val;
}

template <typename T>
vec1<T> cin2vec(size_t size)
{
    vec1<T> vec1;
    for (size_t i = 0; i < size; ++i) {
        vec1.push_back(cin2var<T>());
    }
    return vec1;
}

template <typename T>
T ToGrayCode(T num)
{
    return num ^ (num >> 1);
}

using bs24 = bitset<24>;

int FindBit(const bs24& bs)
{
    for (size_t i = 0; i < 24; ++i) {
        if (bs.test(i)) {
            return i;
        }
    }
    return -1;
}

int main()
{
    while (true) {
        const auto N(cin2var<size_t>());
        if (!N) break;
        const auto   Ns(cin2vec<int>(N));
        const size_t count     = 1 << N;
        int          sum_a     = 0;
        int          sum_b     = accumulate(Ns.begin(), Ns.end(), 0);
        int          diff_min  = sum_b;
        size_t       prev_gray = 0;
        for (size_t i = 1; i < count; ++i) {
            const size_t gray = ToGrayCode(i);
            const size_t diff = gray ^ prev_gray;
            const bs24 bs_diff(diff);
            const int  diff_pos = FindBit(bs_diff);
            const auto pow = Ns[diff_pos];
            const bs24 bs_gray(gray);
            if (bs_gray.test(diff_pos)) {
                // move A <- B
                sum_a += pow;
                sum_b -= pow;
            }
            else {
                // move A -> B
                sum_a -= pow;
                sum_b += pow;
            }
            const auto diff_ab = abs(sum_a - sum_b);
            if (diff_min > diff_ab) {
                diff_min = diff_ab;
            }
            prev_gray = gray;
        }
        cout << diff_min << endl;
    }
    return 0;
}

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

int n; //登録者数
int w[30]; //各登録者の戦闘力

int dfs(int A, int B, int m){//AはグループAの総力、BはグループBの総力、mは登録者の順番
    int a, b;
    if(m >= n) return abs(A - B); 
    a = dfs(A + w[m], B, m + 1);
    b = dfs(A, B + w[m], m + 1); 
    return min(a, b);
}

int main(void){
    while(true){
        cin >> n;
        if(n == 0) break;
        rep(i, n) cin >> w[i];

        cout << dfs(0, 0, 0) << endl;
    }
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int n;
int mn;
int a[21];

void dfs(int battle , int depth){
	if(depth==n){
		mn=min( mn , abs(battle));
	} else {
		//cout << "left >battle:" << abs(battle+a[depth]) << " depth:" << depth << endl;
		dfs(battle+a[depth] , depth+1);
		//cout << "right>battle:" << abs(battle-a[depth]) << " depth:" << depth << endl;
		dfs(battle-a[depth] , depth+1);
	}
}


int main(){
	while(cin>>n , n!=0){
		for(int i=0;i<n;++i){
			cin >> a[i];
		}
		mn=1000001;
		dfs(0 , 0);
		cout << mn << endl;
	}
	return 0; 
}

C++: #include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
// int dx[]={0,1,0,-1};
// int dy[]={1,0,-1,0};
struct edge{int to,cost;};
//typedef pair<int,int> P;

bool isupper(char c){if('A'<=c&&c<='Z')return 1;return 0;}
bool islower(char c){if('a'<=c&&c<='z')return 1;return 0;}
bool isPrime(int x){if(x==1)return 0;if(x==2)return 1;if(x%2==0)return 0;for(int i=3;i*i<=x;i++)if(x%i==0)return 0;return 1;}
bool iskaibun(string s){for(int i=0;i<s.size()/2;i++)if(s[i]!=s[s.size()-i-1])return 0;return 1;}
bool isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)||islower(c));}
void printvi(vector<int> v){rep(i,0,v.size()){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
void printvil(vector<int> v){rep(i,0,v.size()){cout<<v[i]<<endl;}}
void printvvi(vector<vector<int>> v){
    rep(i,0,v.size()){
        rep(j,0,v[i].size()){
            if(j)cout<<" ";
            cout<<v[i][j];
        }
        cout<<endl;
    }
}
void printvstr(vector<string> v){
    rep(i,0,v.size()){
        cout<<v[i]<<endl;
    }
}

int gcd(int a,int b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    else return gcd(b,a%b);
}

struct S{
    int id,team,ac,pena;
};

string alllower(string s){
    string ret;
    rep(i,0,s.size()){
        if(isupper(s[i]))ret+=char(s[i]+'a'-'A');
        else ret+=s[i];
    }
    return ret;
}

string first_upper(string s){
    string ret;
    rep(i,0,s.size()){
        if(i==0 && islower(s[i]))ret+=char(s[i]-('a'-'A'));
        else ret+=s[i];
    }
    return ret;
}

signed main (){
    int n;
    for(;;){
        cin>>n;
        if(n==0)break;
        vector<int> v(n);
        rep(i,0,n)cin>>v[i];
        int ans=1e9;
        for(int s=0;s<(1<<n);s++){
            int a,b;
            a=b=0;
            rep(i,0,n){
                if(s&(1<<i))a+=v[i];
                else b+=v[i];
            }
            ans=min(ans,abs(a-b));
        }
        cout<<ans<<endl;
    }



    
}
/*
*/

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;

int main(){
	
	int n;
	
	while(cin>>n,n!=0){
		int a=0,b=0;
		vector<int> s(n);
	
	for(int i=0;i<n;i++)
		cin>>s[i];
	
	sort(s.begin(),s.end(),greater<int>());
	
	
		for(int i=0;i<n;i++){
			if(s[i]>0){
			if(a>b)
				b+=s[i];
			else
				a+=s[i];
		}
			else{
				if(a>b)
				a+=s[i];
			else
				b+=s[i];
			}
		}
		cout<<abs(a-b)<<endl;
	}
}