Go: package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	for {
		n := getNextInt(scanner)
		if n == 0 {
			break
		}
		sum := 0
		ll := make([]int, 1)
		rr := make([]int, 1)
		for i := 0; i < n/2; i++ {
			a := getNextInt(scanner)
			sum += a
			ll = merge(ll, a)
		}
		for i := n / 2; i < n; i++ {
			a := getNextInt(scanner)
			sum += a
			rr = merge(rr, a)
		}
		r := len(rr) - 1
		var ans cint
		ans = math.MaxInt32
		for l := 0; l < len(ll); l++ {
			for r >= 0 && ll[l]+rr[r] > 0 {
				r--
			}
			if r >= 0 {
				ans.minAs(cint(ll[l] + rr[r]).abs())
			}
			if r+1 < len(rr) {
				ans.minAs(cint(ll[l] + rr[r+1]).abs())
			}
		}
		fmt.Fprintln(writer, ans)
	}
}

type cint int64

func (ct *cint) minAs(x cint) *cint {
	*ct = ct.min(x)
	return ct
}
func (ct *cint) maxAs(x cint) *cint {
	*ct = ct.max(x)
	return ct
}
func (ct cint) min(x cint) cint {
	if ct > x {
		return x
	}
	return ct
}
func (ct cint) max(x cint) cint {
	if ct < x {
		return x
	}
	return ct
}
func (ct cint) abs() cint {
	return ct.max(-ct)
}
func merge(aa []int, a int) []int {
	n := len(aa)
	bb := make([]int, n)
	cc := make([]int, 0)
	for i := 0; i < n; i++ {
		bb[i] = aa[i] + a
		aa[i] = aa[i] - a
	}
	i := 0
	j := 0
	for i < n || j < n {
		if first(aa, bb, i, j, n) {
			cc = append(cc, aa[i])
			i++
			continue
		}
		cc = append(cc, bb[j])
		j++
	}
	return cc
}
func first(aa, bb []int, a, b, n int) bool {
	if a == n {
		return false
	}
	if b == n {
		return true
	}
	return aa[a] < bb[b]
}

C++: #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n,p[20],s[1<<20],s1[1<<10];

int main(){
	while(cin>>n){
		if(!n)return 0;
		int n2=n/2;
		fill(s1,s1+(1<<n2),0);
		int sum=0,dif=1e+9;
		for(int i=0;i<n;i++){cin>>p[i];sum+=p[i];}
		if(n<11){
			fill(s,s+(1<<n),0);
			for(int i=0;i<1<<n;i++){
				int sss=0;
				for(int j=0;j<n;j++)if((i/(1<<j))%2)sss+=p[j];
				dif=min(dif,abs(sss*2-sum));
			}
			cout<<dif<<endl;
		}
		else{
		for(int i=0;i<1<<n2;i++){
			for(int j=0;j<n2;j++)if(i>>j&1)s1[i]+=p[j];
		}
		sort(s1,s1+(1<<n2));
		int ans=1e+9;
		for(int i=0;i<1<<(n-n2);i++){
			int ss=0;
			
			for(int j=0;j<n-n2;j++)if(i>>j&1)ss+=p[j+n2];
			ans=min(ans,min(abs((*lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)+ss)*2-sum),abs((*(lower_bound(s1,s1+(1<<n2),(sum+1)/2-ss)-1)+ss)*2-sum)));
		}
		cout<<ans<<endl;
		}
	}
}

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

#define INF 1e+10;

int n,ans,a[21];

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>

using namespace std;

int main(){
	int n;
	long p[20], all = 0, party1=0,min;
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> p[i];
		all += p[i];
	}
	min = all;
	for (long bit = 1; bit < 1 << 20; bit++){
		for (int i = 0; i < 20; i++){
			if (bit >> i & 1){
				party1 += p[i];
			}
		}
		if (min > all - party1){
			min = all - party1;
		}
	}
	cout << min << endl;
	return 0;
}

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

int n, a[21], ans;

void dfs(int sum, int k){
	if( k == n ){
		ans = min( ans , abs(sum) );
	}else{
		dfs(sum + a[k], k+1 );
		dfs(sum - a[k], k+1 );
	}
}

int main(){
	while( cin >> n , n ){
		for(int i=0 ; i < n ; i++ ){
			cin >> a[i];
		}
		ans = 1e+9;
		dfs(0,0);
		cout << ans << endl;
	}
}

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

int n , v[20];

map<int,int> memo[20];

int f(int pos , int sum){
	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;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef set<int> S;

int main(){
  int n;
  while(cin>>n,n){
    int a[20],sum=0;
    for(int i=0;i<n;i++)cin>>a[i],sum+=a[i];
    
    S mp;
    for(int i=0;i<n;i++)
      for(S::iterator it=mp.begin();it!=mp.end();it++)mp.insert((*it)+a[i]);
    
    int ans=1e9;
    for(S::iterator it=mp.begin();it!=mp.end();it++)ans=min(ans,sum-(*it));
    cout <<ans<<endl;
  }
  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 = 0,b = 0;
		int nl[20];
		for(int i = 0;i < n;i++){
			cin >> nl[i];
		}
		
		sort(nl,nl+n);
		reverse(nl,nl+n);
		
		for(int i = 0;i < n;i++){
			if(a > b){
				b += nl[i];
			}else{
				a += nl[i];
			}
		}
		ans.push_back(abs(a-b));
	}
	for(int i = 0;i < ans.size();i++) cout << ans[i] << endl;
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int n;
int p[20];
int mind;

void dfs(int a=0, int b=0, int i=0)
{
  if (i == n){
    mind = min(mind, abs(a - b));
    return;
  }
  dfs(a + p[i], b, i + 1);
  dfs(a, b + p[i], i + 1);
}

int main()
{
  while (cin >> n, n){
    for (int i = 0; i < n; i++){
      cin >> p[i];
    }
    mind = INT_MAX;
    dfs();
    cout << mind << endl;
  }
}

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

int main()
{
  int n;
  for(;cin>>n,n;)
    {
      long long int data[n];
      long long int ans=0;
      for(int i=0;i<n;i++)
	{
	  cin>>data[i];
	  ans+=data[i];
	}
      for(long long int i=0;i<(1<<n);i++)
	{
	  //	  cout<<i<<endl;
	  long long int sum[2]={0,0};
	  for(int j=0;j<n;j++)
	    {
	      //	      cout<<(1<<j)<<endl;
	      if((i&(1<<j))==0)
		sum[0]+=data[j];
	      else
		sum[1]+=data[j];
	    }
	  //	  cout<<sum[0]<<" "<<sum[1]<<endl;
	  ans=min(ans,abs(sum[0]-sum[1]));
	  if(ans==0)
	    break;
	}


      cout<<ans<<endl;
    }

}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int f(int a[],int b[],int p,int m,int n,int s){
  int i;
  int c[20];
  int mn=abs(2*s-m);
  if(s<=m){
    for(i=p;i<n;i++){
      if(b[i]==0){
	memcpy(c,b,sizeof(c));
	c[i]=1;
	int t=f(a,c,i+1,m,n,s+a[i]);
	if(mn==-1||mn>t)
	  mn=t;
      }
    }
  }
  return mn;
}
int main(){
  int i;
  int a[20],b[20],n;
  int sm;
  memset(b,0,sizeof(b));
  while(cin>>n&&n){
    sm=0;
    for(i=0;i<n;i++){
      cin>>a[i];
      sm+=a[i];
    }
    cout<<f(a,b,0,sm,n,0)<<endl;
  }
  return 0;
}