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<climits>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>

using namespace std;

int main(){
    int n;
    int a[22];
    while(cin>>n,n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int l=0;
        int mi=INT_MAX;
        while(l<(2<<n)){
            int p=0,q=0;
            for(int i=0;i<n;i++){
                if((l>>i)&1)
                    p+=a[i];
                else
                    q+=a[i];
            }
            mi=min(abs(q-p),mi);
            l++;
        }
        cout<<mi<<endl;
    }
    
}

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

signed main(){
int a;
while(cin>>a,a){
	int sum=0;
	vector<vector<int>>c(a+1);
	c[0].push_back(0);
	for(int b=0;b<a;b++){
		int e;
		scanf("%lld",&e);
		sum+=e;
		for(int d=0;d<c[b].size();d++){
			c[b+1].push_back(c[b][d]);
			c[b+1].push_back(c[b][d]+e);
		}
	}
	int MIN=LLONG_MAX/3;
	for(int x=0;x<c[a].size();x++){
		MIN=min(MIN,abs(c[a][x]-(sum-c[a][x])));
	}
	printf("%lld\n",MIN);
}
}

C++: #include<cstdio>
#include<sstream>
#include<string>

#define reps(i,f,n) for(int i = f; i < int(n); i++)
#define rep(i,n) reps(i,0,n)

using namespace std;


int main(void){
	int n;
	while(cin >> n){
		int ans = 100000000;
		int a[32]={0};
		if(!n)break;
		rep(i,n)cin >> a[i];
		
		rep(i,1<<n){
			int ga = 0,gb = 0;
			for(int j = 1,index = 0; j < 1<<n; j = j<<1,index++){
				if(i&j){
					ga+=a[index];
				}else{
					gb+=a[index];
				}
			}
			ans = min(abs(ga-gb), ans);
		}
		
		printf("%d\n", ans);
	}
}

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

int main(){
  int n;
  while(cin>>n,n){
    int a[20];
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a,a+n,greater<int>());
    int A=0,B=0;
    for(int i=0;i<n;i++){
      if(A<B)A+=a[i];
      else B+=a[i];
    }
    cout <<abs(A-B)<<endl;
  }
  return 0;
}

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

using namespace std;

const int INF = 1e+9;

int n, a[21], ans;

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 <cmath>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n, sum; vector<int> a;
    
    while(true)
    {
        cin >> n;
        
        if(n == 0) { break; }
        
        a = vector<int>(n); sum = 0;
        
        for(int i = 0; i < n; i++)
        {
            cin >> a[i]; sum += a[i];
        }
        
        int min_ = (1 << 29);
        
        for(int i = 0; i < (1 << n); i++)
        {
            int sum2 = 0;
            
            for(int j = 0; j < n; j++)
            {
                if(i & (1 << j))
                {
                    sum2 += a[j];
                }
            }
            
            cout << sum2 << endl;
            
            min_ = min(abs(2 * sum2 - sum), min_);
        }
        
        cout << min_ << endl;
    }
    
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100000
using namespace std;

int main()
{
  while(1)
    {
      int n,in,A,B,C[N];
      cin >> n;
      if(n == 0)break;
      for(int i=0;i<n;i++)
	{
	  cin >> C[i];
	}
      sort(C,C+n);
      //for(int i=0;i<n;i++)cout << C[i] << " ";
      //cout << endl;
      A=C[n-1];     
      B=C[n-2];

      //cout << A << " " << B << endl;
      for(int i=n-3;i>=0;i--)
	{
	  if(A>B)
	    {
	      B+=C[i];
	    }
	  else if(B>A)
	    {
	      A+=C[i];
	    }
	  else 
	    {
	      A+=C[i];
	    }
	}
      int ans;
      ans = max(A,B) - min(A,B);
      cout << ans << endl;

    }



}

C++: #include<stdio.h>

int a[20];

int main(){
  int n;
  for(;scanf("%d",&n)&&n;){
    for(int i=0;i<n;++i)scanf("%d",a+i);
    int mi=1e9;
    for(int b=0;b<1<<n;++b){
      int d=0;
      for(int i=0;i<n;++i)
        d+=b&1<<i?a[i]:-a[i];
      if(d<0)d=-d;
      if(mi>d)mi=d;
    }
    printf("%d\n",mi);
  }
}

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;
}