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<cstdio>
#include<cstdlib>

int n,deta[30],kon,pon;
int sa = 1000000;
int max;
void flat(int joutai,int kon,int pon);

using namespace std;
int main(void)
{
	while(scanf("%d",&n))
	{
		max = 0;
		if(n == 0) break;
		for(int i = 0;i<n;i++)
		{
			scanf("%d",&deta[i]);
			max += deta[i];
		}
			flat(0,0,0);
			printf("%d\n",sa);
	}
	return 0;
}

void flat(int joutai,int pon,int kon)
{
	if(pon + kon == max)
	{
		if(sa > abs(pon-kon)) sa = abs(pon - kon);
		return;
	}
		flat(joutai+1,kon+deta[joutai],pon);
		flat(joutai+1,kon,pon+deta[joutai]);
}

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

int main() {
  int n;
  
  while(cin >> n, n) {
    int data[20]={}, all = 0;
    for(int i=0; i<n; i++) {
      cin >> data[i];
      all += data[i];
    }
    
    int ans = 1<<23;
    for(int i=0; i<(1<<n); i++) {
      int a = 0;
      for(int j=0; j<n; j++) {
	if((i>>j) & 1) {
	  a += data[j];
	  ans = min(ans, abs(a-(all-a)));
	}
      }
    }
    cout << ans << endl;
  }
  
  return 0;
}

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

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

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

using namespace std;

int A[32];

int getMini(int idx, int costA, int costB)
{
	if (idx == -1) return (abs(costA - costB));
	
	return (min(getMini(idx - 1, costA + A[idx], costB),
				getMini(idx - 1, costA, costB + A[idx])));
}

int main()
{
	int n;
	
	while (scanf("%d", &n) && n){
		for (int i = 0; i < n; i++){
			scanf("%d", A + i);
		}
		
		printf("%d\n", getMini(n - 1, 0, 0));
	}
	
	return (0);
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e15+9
#define MOD 1000000007
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

ll n, table[100];

ll ans;

void solve(int num, int asum, int bsum){
	if (num == n){
		ans = min(ans, (ll)(abs(asum - bsum)));
		return;
	}

	solve(num + 1, asum + table[num], bsum);
	solve(num + 1, asum, bsum + table[num]);
}

int main(){
	while (cin >> n&&n){
		ans = INF;
		REP(i, n){
			cin >> table[i];
		}
		solve(0, 0, 0);
		cout << ans << endl;
	}
}

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

int main(){
  int n, a[100], suma=0, sumb=0;

  while(1){
    cin >> n;
    if(n == 0) break;

    for(int i = 0;i<n;i++){
      cin >> a[i];
    }

    sort(a, a+n, std::greater<int>());

    for(int i = 0;i<n;i++){
      if(suma <= sumb){
        suma += a[i];
      }else{
        sumb += a[i];
      }
    }

    cout << abs(suma - sumb) << endl;

    suma = 0;
    sumb = 0;
  }

  return 0;
}

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

int main(){
  int n;
  int a[30];

  while(cin >> n ,n){
    for(int i=0;i<n;i++)cin >> a[i];

    int ans = 30000000;
    for(int i=0;i<(1<<n);i++){
      int A=0,B=0;
      for(int j=0;j<n;j++){
	if((i>>j)&1)A += a[j];
	else B += a[j];
      }
      ans = min(ans,abs(A-B));
    }
    cout << ans << endl;
  }
}

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

using namespace std;

int main()
{
	int n;
	
	for(;;)
	{
		vector<int> num;
		cin >> n;
		if(n==0)
			break;
		for(int i=0;i<n;i++)
		{
			int a;
			cin >> a;
			num.push_back(a);
		}
		
		sort(num.begin(),num.end());
		
		int a=0,b=0;
		int a_f=1;
		
		if(num.size()==2)
		{
			cout << num[1]-num[0] << endl;
		}
		else if(num.size()==3)
		{
			cout << num[2]-num[1]-num[0] << endl;
		}
		else
		{
			if(num.size()%2==0)
			{
				for(int i=0,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
			}
			else
			{
				for(int i=1,j=num.size()-1;i<j;i++,j--)
				{
					if(a_f==1)
					{
						a+=num[i]+num[j];
						a_f=0;
					}
					else
					{
						b+=num[i]+num[j];
						a_f=1;
					}
				}
				if(a<b)
					a+=num[0];
				else
					b+=num[0];
			}
			
			int ans_a,ans_b;
			ans_a=max(a,b);
			ans_b=min(a,b);
//			cout << "ans_a:" << ans_a << " ans_b:" << ans_b << endl;
			cout << ans_a-ans_b << endl;
		}
	}
}

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

using namespace std;

const int INF=1e+9;
int n,ans;
int a[21];


void dfs(int diff,int k);

int main(){
	
	while(cin>>n&&n!=0){

		for(int i=0;i<n;i++){
			cin>>a[i];
		}
		ans=INF;
		dfs(0,0);
		cout<<ans<<endl;
	}
	
}

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

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

using namespace std;

int main(){

	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 <algorithm>
using namespace std;

#define MAX_N 20

int n;
int power[MAX_N];

int min_diff(int a, int b, int p) {
    if (p == n) return abs(a-b);
    return min(min_diff(a+power[p], b, p+1), min_diff(a, b+power[p], p+1));
}

int main() {
    while (cin >> n) {
        if (n == 0) break;
        for (int i = 0; i < n; i++) {
            cin >> power[i];
        }
        cout << min_diff(0, 0, 0) << endl;
    }
    return 0;
}