Problem:
Problem 05:
Split Up!
勇者ポン太とその親友同じく勇者ゴン太は、これから壮大な冒険へと出発するために、それぞれ仲間を求めてルイーダの酒場へやって来ました。酒場には、冒険に出たくてうずうずしている武闘家や僧侶、魔法使いがたくさん居ます。
心やさしいゴン太はポン太を気遣い、「先に仲間を選んでいいよ。」と言いました。
一方、ポン太もゴン太が心配でなりません。「いや、君が先に選んでいいよ。」と言い返しました。
お互い譲り合いが続き、ついにルイーダがこう提案しました。「じゃあ、ここにいる
n
人の登録者をそれぞれのパーティの合計戦闘力がなるべく均等になるように、この"パーティ分けマシン"で分けてあげるわ。」
あなたに与えられた仕事は、パーティ分けマシンに組み込まれているプログラムを作成することです。
このプログラムは
n
個の整数を入力し、それらを２つのグループ
A
,
B
に分けたときの、
A
に含まれる整数の合計値と
B
に含まれる整数の合計値の差の最小値を出力しなければなりません。
このマシンのおかげで、ポン太とゴン太は仲良く仲間を見つけ、壮大な冒険へと出かけて行きました・・。
Input
入力として複数のデータセットが与えられます。各データセットは以下の形式で与えられます：
n
(登録者の数： 整数)
a
1
a
2
...
a
n
（各登録者の戦闘力： 空白区切りの整数）
n
は 20 以下であり、各登録者の戦闘力は 100 万を越えません。
n
が 0 のとき入力の終わりとします。
Output
各データセットに対して、最小値を1行に出力して下さい。
Sample Input
5
1 2 3 4 5
4
2 3 5 7
0
Output for the Sample Input
1
1

Submitted solutions:

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

/*
  与えられた n 個の整数を 2 つのグループに分け，
  グループ A の合計とグループ B の合計の差の最小値を求める
  ブールマスク全探索による典型的な部分和分割問題の解法。
*/

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(true){
        int n;
        cin >> n;
        if(!cin || n == 0) break;

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

        long long best = LLONG_MAX;
        // 全ての部分集合をビットマスクで列挙
        // A に入れる要素を 1、B に入れる要素を 0 とする
        int total = 1 << n;
        for(int mask = 0; mask < total; mask++){
            long long sumA = 0;
            // mask のビットごとに A の合計を計算
            for(int i = 0; i < n; i++){
                if(mask & (1 << i)) sumA += a[i];
            }
            long long sumB = S - sumA;
            long long diff = llabs(sumA - sumB);
            best = min(best, diff);
        }

        cout << best << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 3344
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <limits>

int main() {
    int n;
    while (std::cin >> n && n != 0) {
        std::vector<int> powers(n);
        for (int i = 0; i < n; ++i) {
            std::cin >> powers[i];
        }

        int minDiff = std::numeric_limits<int>::max();

        // 全ての可能な分割を試す
        for (int mask = 0; mask < (1 << n); ++mask) {
            int groupA = 0, groupB = 0;

            // 各ビットをチェックしてグループに分ける
            for (int i = 0; i < n; ++i) {
                if (mask & (1 << i)) {
                    groupA += powers[i];
                } else {
                    groupB += powers[i];
                }
            }

            // 2つのグループの合計値の差を計算し、最小値を更新
            int diff = std::abs(groupA - groupB);
            minDiff = std::min(minDiff, diff);
        }

        std::cout << minDiff << std::endl;
    }

    return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 3316
final_rank: 16

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

#ifdef DEBUG_
#include <compe/debug.hpp>
#else
#define dump(...)
#endif

#define FastIO cin.tie(nullptr), ios_base::sync_with_stdio(false);
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define output(msg) cout << (msg) << '\n'
#define die(msg)         \
  do {                   \
    cout << msg << endl; \
    exit(0);             \
  } while (0)
#define all(k) k.begin(), k.end()
#define INFi 1 << 30
#define INFll 1LL << 60

template <typename T> bool chmax(T& a, const T& b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> bool chmin(T& a, const T& b) {
  return ((a > b) ? (a = b, true) : false);
}

using llint = long long int;

void solve(int n) {
  vector<llint> ai(n);
  rep(i, n) cin >> ai[i];

  llint ans{INFll};
  for (int bit = 0; bit < (1 << n); ++bit) {
    llint left{}, right{};
    rep(i, n) {
      if (bit & (1 << i)) {
        left += ai[i];
      } else {
        right += ai[i];
      }
    }
    chmin(ans, abs(left - right));
  }
  output(ans);
}

int main() {
  FastIO;
  int n;
  while (cin >> n) {
    if (n == 0) break;
    solve(n);
  }
}
accuracy: 1
time_ms: 570
memory_kb: 3212
final_rank: 17

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n, n) {
        vector<int> a(n);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            sum += a[i];
        }
        int res = sum;
        for (int i = 0; i < (1<<n); i++) {
            int s = 0;
            for (int j = 0; j < n; j++) {
                if ((1<<j) & i) {
                    s += a[j];
                }
            }
            res = min(res, abs(sum-s-s));
        }
        cout << res << endl;
    }
}
accuracy: 1
time_ms: 470
memory_kb: 3216
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int INF = 10000000;

int n;
vector<int> v;

int ans = INF;

void calc(int i, int s, int t)
{
	if (i == n)
	{
		ans = min(ans, abs(s - t));
		return;
	}
	calc(i + 1, s + v[i], t);
	calc(i + 1, s, t + v[i]);
}

int main()
{
	int s = 0, t = 0;
	while (cin >> n && n > 0)
	{
		v = vector<int>(n, 0);
		ans = INF;
		for (int i = 0; i < n; i++)
		{
			cin >> v[i];
		}
		calc(0, 0, 0);
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3424
final_rank: 2

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

int main(){
    int n;
    while(cin >> n, n){
        vector<int> a(n);
        int v, ans = 1 << 30, s = 0;
        for(int i = 0; i < n; i++){
            cin >> a[i];
            s += a[i];
        }
        for(int i = 0; i < (1 << n); i++){
            v = 0;
            for(int j = 0; j < n; j++){
                if(i >> j & 1)v += a[j];
            }
            ans = min(ans, abs(2 * v - s));
        }
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 470
memory_kb: 3428
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define FastIO cin.tie(nullptr), ios_base::sync_with_stdio(false);
#define REP(i, start, end) for (int i = start; i < (int)(end); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;

void solve(int n) {
  vector<long long int> score(n);
  for (auto &s : score) cin >> s;

  long long int diff = LLONG_MAX;

  for (int bit = 0; bit < (1 << n); bit++) {
    long long int a = 0, b = 0;

    for (int i = 0; i < n; i++) {
      if (bit & (1 << i)) {
        a += score[i];
      } else {
        b += score[i];
      }
    }
    diff = min(diff, abs(a - b));
  }
  cout << diff << '\n';
}

int main() {
  FastIO;

  int t;
  while (cin >> t) {
    if (t == 0) break;
    solve(t);
  }
}
accuracy: 1
time_ms: 570
memory_kb: 3444
final_rank: 18

code_snippet: #line 1 "library/template/template.hpp"
//#pragma GCC target("avx")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define overload4(a,b,c,d,e,...) e
#define overload3(a,b,c,d,...) d
#define rep1(a) for(ll i=0;i<(ll)(a);i++)
#define rep2(i,a) for(ll i=0;i<(ll)(a);i++)
#define rep3(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep4(i,a,b,c) for(ll i=(ll)(a);i<(ll)(b);i+=(ll)(c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(a) for(ll i=(ll)(a)-1;i>=0;i--)
#define rrep2(i,a) for(ll i=(ll)(a)-1;i>=0;i--)
#define rrep3(i,a,b) for(ll i=(ll)(b)-1;i>=(ll)(a);i--)
#define rrep(...) overload3(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define fore(...) for (auto&& __VA_ARGS__)
#define all1(i) begin(i),end(i)
#define all2(i,a) begin(i),begin(i)+a
#define all3(i,a,b) begin(i)+a,begin(i)+b
#define all(...) overload3(__VA_ARGS__,all3,all2,all1)(__VA_ARGS__)
#define rall(n) (n).rbegin(),(n).rend()
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
#define pb push_back
#define eb emplace_back
#define END(...) {print(__VA_ARGS__);return;}
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using vl=vector<ll>;
using vi=vector<int>;
using vs=vector<string>;
using vc=vector<char>;
using vvl=vector<vl>;
using pi=pair<int,int>;
using pl=pair<ll,ll>;
using vvc=vector<vc>;
using vd=vector<double>;
using vp=vector<pl>;
using vb=vector<bool>;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const ll MOD=1000000007;
const ll mod=998244353;
const ld EPS=1e-8;
const ld PI=3.1415926535897932384626;
template<typename T,typename U>
ostream &operator<<(ostream&os,const pair<T,U>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T,typename U>
istream &operator>>(istream&is,pair<T,U>&p){is>>p.first>>p.second;return is;}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&v){for(auto it=v.begin();it!=v.end();){os<<*it<<((++it)!=v.end()?" ":"");}return os;}
template<typename T>
istream &operator>>(istream&is,vector<T>&v){for(T &in:v){is>>in;}return is;}
void scan(){}
template<class Head,class... Tail>
void scan(Head&head,Tail&... tail){cin>>head;scan(tail...);}
template<class T>
void print(const T &t){cout<<t<<'\n';}
template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail){cout<<head<<' ';print(tail...);}
template<class... T>
void fin(const T &... a){print(a...);exit(0);}
template<typename T,typename U>
inline bool chmax(T&a,U b){return a<b&&(a=b,true);}
template<typename T,typename U>
inline bool chmin(T&a,U b){return a>b&&(a=b,true);}
template<typename T>
class infinity{
  public:
  static const T MAX=numeric_limits<T>::max();
  static const T MIN=numeric_limits<T>::min();
  static const T value=numeric_limits<T>::max()/2;
  static const T mvalue=numeric_limits<T>::min()/2;
};
#if __cplusplus <= 201402L
template<class T>const T infinity<T>::value;
template<class T>const T infinity<T>::mvalue;
template<class T>const T infinity<T>::MAX;
template<class T>const T infinity<T>::MIN;
#endif
template<typename T>const T INF=infinity<T>::value;
const long long inf=INF<ll>;
inline int popcnt(ull x){
#if __cplusplus>=202002L
return popcount(x);
#endif
x=(x&0x5555555555555555)+((x>>1)&0x5555555555555555);x=(x&0x3333333333333333)+((x>>2)&0x3333333333333333);x=(x&0x0f0f0f0f0f0f0f0f)+((x>>4)&0x0f0f0f0f0f0f0f0f);x=(x&0x00ff00ff00ff00ff)+((x>>8)&0x00ff00ff00ff00ff);x=(x&0x0000ffff0000ffff)+((x>>16)&0x0000ffff0000ffff);return (x&0x00000000ffffffff)+((x>>32)&0x00000000ffffffff);
}
template<typename T,typename=void>
struct is_specialize:false_type{};
template<typename T>
struct is_specialize<T,typename conditional<false,typename T::iterator, void>::type>:true_type{};
template<typename T>
struct is_specialize<T,typename conditional<false,decltype(T::first),void>::type>:true_type{};
template<typename T>
struct is_specialize<T,enable_if_t<is_integral<T>::value,void>>:true_type{};
void dump(const char&t){cerr<<t;}
void dump(const string&t){cerr<<t;}
void dump(const bool&t){cerr<<(t?"true":"false");}
template <typename T,enable_if_t<!is_specialize<T>::value,nullptr_t> =nullptr>
void dump(const T&t){cerr<<t;}
template<typename T>
void dump(const T&t,enable_if_t<is_integral<T>::value>* =nullptr){string tmp;if(t==infinity<T>::value||t==infinity<T>::MAX)tmp="inf";if(t==infinity<T>::mvalue||t==infinity<T>::MIN)tmp="-inf";if(tmp.empty())tmp=to_string(t);cerr<<tmp;}
template <typename T>
void dump(const T&t,enable_if_t<!is_void<typename T::iterator>::value>* =nullptr){cerr<<"{";for(auto it=t.begin();it!=t.end();){dump(*it);cerr<<(++it==t.end()?"":",");}cerr<<"}";}
template<typename T,typename U>
void dump(const pair<T,U>&t){cerr<<"(";dump(t.first);cerr<<",";dump(t.second);cerr<<")";}
void trace(){cerr<<endl;}
template<typename Head,typename... Tail>
void trace(Head&&head,Tail&&... tail){dump(head);if(sizeof...(tail))cerr<<",";trace(forward<Tail>(tail)...);}
#ifdef ONLINE_JUDGE
#define debug(...) (void(0))
#else
#define debug(...) do{cerr<<#__VA_ARGS__<<"=";trace(__VA_ARGS__);}while(0)
#endif
struct IOSetup{IOSetup(){cin.tie(nullptr);ios::sync_with_stdio(false);cout.tie(0);cout<<fixed<<setprecision(12);cerr<<fixed<<setprecision(12);}};
/**
 * @brief Template(テンプレート)
*/
#line 2 "code.cpp"
void solve(){
  LL(n);
  if(!n)exit(0);
  vl a(n);
  cin>>a;
  bitset<20000001>bit;
  bit[0]=1;
  rep(i,n)bit|=(bit<<a[i]);
  ll sum=accumulate(all(a),0LL);
  ll aim=sum/2;
  while(bit[aim]==0){
    aim--;
  }
  print((sum-aim*2));
}
int main(){
  while(true)solve();
}
accuracy: 1
time_ms: 230
memory_kb: 10748
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)


int main() {
    while(1){
        ll N;
        cin>>N;
        if(N==0)return 0;
        vll A(N);
        rep(i,N)cin>>A[i];
        ll an=1e18;
        rep(bit,(1<<N)){
            ll S=0,D=0;
            rep(i,N){
                if(bit&(1<<i))S+=A[i];
                else S-=A[i];
            }
            an=min(an,abs(S));
        }
        cout<<an<<endl;
    }
    
}
accuracy: 1
time_ms: 240
memory_kb: 3340
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

//イテレーション
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto& i : I)
// x:コンテナ
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647           // 2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807  // 9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007             //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x)                                                   \
    for (auto i = x.begin(); i != --x.end(); i++) cout << *i << " "; \
    cout << *--x.end() << endl;

// aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a, ll b) { return (a + (b - 1)) / b; }
ll myfloor(ll a, ll b) { return a / b; }

ll search(ll n, vector<ll> as, ll l, ll asum, ll bsum) {
    if(l == n) {
        return abs(asum-bsum);
    } 
    ll tmp1 = search(n,as,l+1,asum+as[l],bsum); 
    ll tmp2 = search(n,as,l+1,asum,bsum+as[l]);
    return min(tmp1, tmp2);
}

int main() {
    //小数の桁数の出力指定
    // cout<<fixed<<setprecision(10);
    //入力の高速化用のコード
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    ll n;
    cin >> n;
    while(n != 0) {
        vector<ll> as(n, 0);
        REP(i, n) {
            cin >> as[i];
        }
        ll ans = search(n,as,0,0,0);
        cout << ans << endl;
        cin >> n;
    }
    return 0;
}
accuracy: 1
time_ms: 410
memory_kb: 3144
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;
const int inf = 1e9 + 7;

int main(void) {
    int n;
    while(true){
        cin >> n;
        if(n == 0) return 0;

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

        int mini = inf;
        int max_bit = pow(2,n);

        for (int bit = 0; bit < max_bit; ++bit) {
            int A = 0;
            for(int i = 0; i < n; i++)
                if((bit & (1 << i))) A+= a[i];

            mini = min(mini, abs(A - (sum - A)));
        }
        cout << mini << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 540
memory_kb: 3280
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
int main(){
    int N;
    while(true){
        cin >> N;
        if(N == 0)break;
        vector<int>v(N);
        Int ans = 1e9;
        for(auto& e : v) cin >> e;
        Int bit = (1LL << N);
        for(int i = 0; i <= bit; ++i){
            Int p = 0, g = 0;
            for(int j = 0; j < N; ++j){
                (((1LL  << j) & i) > 0  ? p : g) += v[j];
                
            }
            ans = min(ans,abs(p - g));
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 540
memory_kb: 3372
final_rank: 15

code_snippet: #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>
//#include <boost/multiprecision/cpp_int.hpp>
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF (1LL<<40)
#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 int long long
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define umap unordered_map 
#define re0 return 0
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};
//typedef pair<int,int> P;
const string SP = " ";
const string TAB = "\t"; 
const long long MOD=1000000007LL;
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 isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)||islower(c));}
bool is_kaibun(string s){string rs = s;reverse(all(rs));if(s == rs)return true;else return false;}
std::random_device rnd;
std::mt19937 mt(rnd());
const int RAND_LIMIT = 1000000;
std::uniform_int_distribution<> rand_mt(0, RAND_LIMIT); // rand_mt(mt);

inline double getTime() {
    using namespace std::chrono;
    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
template<typename T> bool chmax(T &m, const T q) {
    if (m < q) {m = q; return true;} else return false; }
template<typename T> bool chmin(T &m, const T q) {
    if (m > q) {m = q; return true;} else return false; }


template<typename T> 
void printv(vector<T> v, bool is_endl=true){
    for(int i=0;i<v.size();i++){
        if(i)cout<<" ";
        cout<<v[i];
    }
    if(is_endl)cout<<endl;
}
//int INF=1e18;

template<typename T>
void printendl(vector<T> v){
    for(auto date:v)cout<<date<<endl;
}

template<typename T>
void printvv(vector<vector<T>> v){
    for(int i=0;i<v.size();i++){
        for(int j=0;j<v[i].size();j++){
            if(j)cout<<" ";
            cout<<v[i][j];
        }
        cout<<endl;
    }
}
// struct Point{
//     int x,y,num;

//     double e_dis(Point other){
//         return sqrt((x-other.x)*(x-other.x)+(y-other.y)*(y-other.y));
//     }
// };

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

bool is_prime(int x){
  if(x==1)return 0;
  if(x==2)return 1;
  for(int i=2;i*i<=x;i++)if(x%i==0)return 0;
  return 1;
}

//スプリット関数
//split(文字列変数,区切りたい文字);
vector<string> split(string s, char c){
    if(s[s.size()-1]!=c)s+=c;
    vector<string> v;
    int pos=0;
    rep(i,0,s.size()){
        if(s[i]==c){
            v.push_back(s.substr(pos,i-pos));
            pos=i+1;
        }
        if(i==s.size()-1 && s.substr(pos,i-pos)!="")v.push_back(s.substr(pos,i-pos));
    }
    return v;
}

map<int,int> prime_factor(int n){
    map<int,int> ret;
    if(n==1){
        ret[1]=1;
        return ret;
    }
    for(int i=2;i*i<=n;i++){
        while(n%i==0){
            ret[i]++;
            n/=i; 
        }
    }
    if(n!=1)ret[n]=1;
    return ret;
}

struct UnionFind{
    vector<int> par;
    UnionFind(int n){
        par.assign(n,-1);
    }
    int root(int x){
        if(par[x]<0)return x;
        else return par[x]=root(par[x]);
    }

    bool same(int a,int b){
        return root(a)==root(b);
    }

    bool merge(int a,int b){
        a=root(a);
        b=root(b);
        if(a==b)return false;

        if(par[a]>par[b])swap(a,b);
        par[a]+=par[b];
        par[b]=a;
        return true;
    }

    int size(int x){
        return -par[root(x)];
    }
};

vector<bool> eratos(int n){
    vector<bool> prime(n,true);
    prime[0]=false;
    prime[1]=false;
    for(int i=2;i<=n;i++){
        if(prime[i]){
            for(int j=i+i;j<=n;j+=i){
                prime[j]=false;
            }
        }
    }
    return prime;
}



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

        }
        cout<<ans<<endl;

        
    }
}
/*

*/
accuracy: 1
time_ms: 610
memory_kb: 3324
final_rank: 19

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 1000000007 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;

  BIT(int n){
    N = n;
    node.resize(N+10);
  }

  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash {
    static const int base1 = 1007, base2 = 2009;
    static const int mod1 = 1000000007, mod2 = 1000000009;
    vector<long long> hash1, hash2, power1, power2;
    int n;
    // construct
    RollingHash(const string &S) {
        n = (int)S.size();
        hash1.assign(n+1, 0);
        hash2.assign(n+1, 0);
        power1.assign(n+1, 1);
        power2.assign(n+1, 1);
        for (int i = 0; i < n; ++i) {
            hash1[i+1] = (hash1[i] * base1 + S[i]) % mod1;
            hash2[i+1] = (hash2[i] * base2 + S[i]) % mod2;
            power1[i+1] = (power1[i] * base1) % mod1;
            power2[i+1] = (power2[i] * base2) % mod2;
        }
    }
    
    // get hash of S[left:right]
    inline pair<long long, long long> get(int l, int r) const {
        long long res1 = hash1[r] - hash1[l] * power1[r-l] % mod1;
        if (res1 < 0) res1 += mod1;
        long long res2 = hash2[r] - hash2[l] * power2[r-l] % mod2;
        if (res2 < 0) res2 += mod2;
        return {res1, res2};
    }
 
    inline pair<long long, long long> c_shift(int l) {
      auto h1 = get(0, l);
      auto h2 = get(l, n);
      return {(h1.first + h2.first * power1[l]) % mod1, (h1.second + h2.second * power2[l]) % mod2};
    }
 
    // get lcp of S[a:] and T[b:]
    inline int getLCP(int a, int b) const {
        int len = min((int)hash1.size()-a, (int)hash1.size()-b);
        int low = 0, high = len;
        while (high - low > 1) {
            int mid = (low + high) >> 1;
            if (get(a, a+mid) != get(b, b+mid)) high = mid;
            else low = mid;
        }
        return low;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  int n;
  while(cin >> n, n) {
    vector<int> a(n);
    REP(i,n) cin >> a[i];
    int ans = 1e9;
    int t = accumulate(all(a), 0);
    REP(bit, (1<<n)) {
      int sum = 0;
      REP(j,n) if(bit >> j & 1) sum += a[j];

      ans = min(ans, abs((t-sum)-sum));
    }
    cout << ans << '\n';
  }
  return 0;
}
accuracy: 1
time_ms: 470
memory_kb: 3328
final_rank: 7

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long Long;

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<Long> a(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        Long cp = accumulate(a.begin(), a.end(), 0LL);
        Long diff = 1000001 * 20;

        for (int i = 0; i < (1 << n); ++i) {
            Long p = 0;
            for (int j = 0; j < n; ++j) {
                if (i & (1 << j)) {
                    p += a[j];
                }
            }
            diff = min(diff, abs(cp - p - p));
        }
        cout << diff << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 530
memory_kb: 3000
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include<iomanip>
#include<functional>
#include<algorithm>
#include<deque>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define rep(i,n) for(int i = 0;i<n;i++)
#define req(i,n) for(int i = 1;i<=n;i++)
#define rrep(i,n) for(int i = n-1;i>=0;i--)
#define ALL(a) a.begin(),a.end()
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef double d;
typedef pair<int, int> P;
const int inf = 1 << 30;
int main() {
	int n; vector<int> A;
	while (cin >> n, n) {
		int n2 = n / 2,num=0;
		vector<int> a(n);
		rep(i, n) cin >> a[i],num += a[i];
		int ans = num;
		rep(bit, 1 << n) {
			int sum = 0;
			rep(i, n) if (bit & (1 << i)) sum += a[i];
			chmin(ans, abs(sum - num + sum));
		}cout << ans << endl;
	}
}
accuracy: 1
time_ms: 530
memory_kb: 3084
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define precise(i) fixed << setprecision(i)
#define rep(z, n) for (int z = 0; z < (int)(n); z++)
#define llint long long int
using namespace std;

int main() {
  fastIO;
  int tt;

  while (cin >> tt) {
    if (tt == 0) break;

    vector<int> A(tt);
    for (auto &a : A) cin >> a;

    llint diff = LLONG_MAX;
    for (int bit = 0; bit < (1 << tt); bit++) {
      llint a = 0, b = 0;

      for (int i = 0; i < tt; i++) {
        if (bit & (1 << i)) {
          a += A[i];
        } else {
          b += A[i];
        }
      }
      diff = min(diff, abs(a - b));
    }
    cout << diff << '\n';
  }
}
accuracy: 1
time_ms: 540
memory_kb: 3324
final_rank: 14

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a.at(i);
    int ans = 1e9;
    for (int bit = 0; bit < (1 << n); bit++) {
      int x = 0, y = 0;
      for (int i = 0; i < n; i++) {
        if (bit & (1 << i)) x += a.at(i);
        else y += a.at(i);
      }
      ans = min(ans, abs(x - y));
    }
    cout << ans << '\n';
  }
}
accuracy: 1
time_ms: 480
memory_kb: 3024
final_rank: 5

code_snippet: #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
	int n;
	while (cin >> n, n) {
		vector<int> a(n);
		for (int& i : a) cin >> i;
		int ans = INT_MAX, b = pow(2.0, n);
		for (int i = 0; i < b; i++) {
			bitset<20> c = i;
			int d = 0;
			for (int j = 0; j < n; j++) {
				if (c[j]) d += a[j];
				else d -= a[j];
			}
			ans = min(abs(d), ans);
		}
		cout << ans << endl;
	}
}
accuracy: 1
time_ms: 520
memory_kb: 3260
final_rank: 12

code_snippet: #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;
    }
}
accuracy: 1
time_ms: 30
memory_kb: 2992
final_rank: 1