Problem:
ビーカー
いろいろな容量のビーカーが与えられています。はじめに、その中の一番容量の大きなビーカーを一個選び、蛇口から水をいっぱいになるまで注ぎます。つぎに、次のルールにしたがいながら、ビーカーの水を他のビーカーに移し替えていきます。
ビーカーに入っている水は,残さずにすべて他のビーカーに移さなければならない。ただし、一個のビーカーに水を全部移せないときは、複数のビーカーに分けて移してもよい。
ビーカーに水を入れるとき、いっぱいになるまで水を注がなければならない。また、水をこぼしてはならない。
複数のビーカーから同じビーカーに一度に水を注いではならない。
同じビーカーには一度しか水を注いではならない。
このルールにしたがったとき、ビーカーの個数
n
と各ビーカーの容量を入力とし、すべてのビーカーに水を注ぐことができるかどうかを判定して出力するプログラムを作成してください。すべてのビーカーに水を注ぐことができるときは YES (半角英大文字)、できないときは NO (半角英大文字) を出力してください。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
n
c
1
c
2
...
c
n
1 行目にビーカーの個数
n
(1 ≤
n
≤ 50) が与えられます。２行目に
i
番目のビーカーの容量を表す整数
c
i
(1 ≤
c
i
≤ 100) が与えられます。
データセットの数は 105 を超えません。
Output
データセット毎に判定結果を１行に出力します。
Sample Input
10
11 2 23 4 2 12 8 5 2 10
8
2 1 3 11 2 3 1 4
9
5 9 1 2 4 8 17 1 8
8
3 38 9 4 18 14 19 5
1
1
0
Output for the Sample Input
YES
YES
YES
NO
YES

Submitted solutions:

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

ll can(const int n,const vector<int> &c, int i, ll bit, int sum) {
    if (sum == 0) {
        return bit;
    }
    if (i == -1) {
        return -1;
    }
    if ((1LL<<i)&bit) {
        if (sum >= c[i]) {
            ll r = can(n,c,i-1,bit^(1LL<<i),sum-c[i]);
            if (r != -1) {
                return r;
            }
        }
    }
    return can(n,c,i-1,bit,sum);
}

bool dfs(const int n,const vector<int> &c, int i, ll bit, int sum) {
    if (i + 1 == n) {
        return (sum == c[i]);
    }
    if (sum > c.back()) return false;
    // 最後に残す場合
    if (dfs(n,c,i+1,(bit|(1LL<<i)),sum+c[i])) {
        return true;
    }
    // これまでの集合からi番目作る場合
    ll b = can(n,c,i-1,bit,c[i]);
    if (b != -1) {
        return dfs(n,c,i+1,b^(1LL<<i),sum);
    }
    return false;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n,n) {
        vector<int> c(n);
        for (int i = 0; i < n; ++i) {
            cin >> c[i];
        }
        sort(c.begin(), c.end());
        if (n == 1 or dfs(n,c,1,1,c[0])) {
            cout << "YES" << "\n";
        }
        else {
            cout << "NO" << "\n";
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 3344
final_rank: 16

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n = -1;
vector<int> beakers;
vector<bool> final_state;
vector<bool> c_state;


bool dfs1(int i, int c) {
	if (c == 0) {
		return true;
	}
	if (i < 0) {
		return false;
	}

	if (!c_state[i]) {
		return dfs1(i - 1, c);
	}

	if (beakers[i] <= c) {
		c_state[i] = false;
		if (dfs1(i - 1, c - beakers[i])) {
			return true;
		}
		c_state[i] = true;
	}
	return dfs1(i - 1, c);
}


bool dfs0(int i, int c) {
	if (c == 0) {
		c_state = final_state;
		for (int j = 0; j < n; j++) {
			if (!c_state[j]) {
				if (!dfs1(j - 1, beakers[j])) {
					return false;
				}
				c_state[j] = true;
			}
		}
		return true;
	}

	if (c < beakers[i]) {
		return false;
	}

	final_state[i] = true;
	if (dfs0(i + 1, c - beakers[i])) {
		return true;
	}
	final_state[i] = false;
	return dfs0(i + 1, c);
}


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

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

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

		sort(beakers.begin(), beakers.end());
		final_state = vector<bool>(n, false);
		final_state[0] = true;

		cout << (dfs0(1, beakers.back() - beakers[0]) ? "YES" : "NO") << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3176
final_rank: 13

code_snippet: #define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int n = -1;
vector<int> c;
vector<bool> final_state;
vector<bool> water;

bool dfs1(int k, int v) {
	if (v == 0) {
		return true;
	}
	if (k < 0) {
		return false;
	}

	if (!water[k]) {
		return dfs1(k - 1, v);
	}

	if (c[k] <= v) {
		water[k] = false;
		if (dfs1(k - 1, v - c[k])) {
			return true;
		}
		water[k] = true;
	}
	return dfs1(k - 1, v);
}


bool dfs0(int k, int v) {
	if (v == 0) {
		water = final_state;
		for (int i = 0; i < n; i++) {
			if (!water[i]) {
				if (!dfs1(i - 1, c[i])) {
					return false;
				}
				water[i] = true;
			}
		}
		return true;
	}

	if (v < c[k]) {
		return false;
	}

	final_state[k] = true;
	if (dfs0(k + 1, v - c[k])) {
		return true;
	}
	final_state[k] = false;
	return dfs0(k + 1, v);
}


int main() {
#if READ_FROM_FILE
	ifstream ifs("test0.txt");
	cin.rdbuf(ifs.rdbuf());
#endif

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

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

		sort(c.begin(), c.end());
		final_state = vector<bool>(n, false);
		final_state[0] = true;

		cout << (dfs0(1, c.back() - c[0]) ? "YES" : "NO") << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3344
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

struct Solution {
    vector<int> used, used2, v;
    int N;
    bool rec(int cur, int rest){
        if(!rest) return true;
        if(cur < 0) return false;
        if(used2[cur] && v[cur] <= rest){
            used2[cur] = 0;
            if(rec(cur-1, rest-v[cur])) return true;
            used2[cur] = 1;
        }
        return rec(cur-1, rest);
    }

    int ok(int cur, int rest){
        if(!rest){
            used2 = used;
            for (int i = 1; i < N; ++i) {
                if(used2[i]) continue;
                if(!rec(i-1, v[i])) return 0;
                used2[i] = 1;
            }
            return true;
        }else if(rest < v[cur]) return 0;
        used[cur] = 1;
        if(ok(cur+1, rest-v[cur])) return 1;
        used[cur] = 0;
        return ok(cur+1, rest);
    }

    void solve(int n){
        if(n == 1){
            cin >> n;
            puts("YES");
            return;
        }
        N = n;
        used.resize(n, 0);
        fill(used.begin(),used.end(),0);
        v.resize(n);
        for (auto &&i : v) scanf("%d", &i);
        sort(v.begin(),v.end());
        used[0] = 1;
        puts(ok(1, v.back()-v.front()) ? "YES" : "NO");
    }
};

int main() {
    int n;
    Solution s;
    while(cin >> n, n) s.solve(n);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3204
final_rank: 14

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

using namespace std;

int N;
int Volume[50];
bool WaterIn[50], Tmp[50];


bool calculation(int id, int volumeOfCurrent) {
	//cout << "rest:" << volumeOfCurrent << "      :id" << id << endl;

	if (id < -1) {
		return false;
	}
	if (volumeOfCurrent == 0) {
		return true;
	}
	
	if (!Tmp[id]) {
		return calculation(id - 1, volumeOfCurrent);
	}

	if (Volume[id] <= volumeOfCurrent) {
		Tmp[id] = false;
		if (calculation(id - 1, volumeOfCurrent - Volume[id])) {
			return true;
		}
		Tmp[id] = true;
	}
	
	return calculation(id - 1, volumeOfCurrent);
}

bool solve(int beakerId, int rest) {
	
	if (rest == 0) {
		//judge
		copy(WaterIn, WaterIn + N, Tmp);
		/*for (int i = 0; i < N; ++i) {
			Tmp[i] = WaterIn[i];
		}*/
		for (int i = 0; i < N; ++i) {
			if (Tmp[i]) {
				continue;
			}
			if (!calculation(i-1, Volume[i])) {
				return false;
			}
			Tmp[i] = true;
		}
		return true;
	}

	if (Volume[beakerId] > rest) {
		return false;
	}

	WaterIn[beakerId] = true;
	if (solve(beakerId + 1, rest - Volume[beakerId])) {
		return true;
	}
	WaterIn[beakerId] = false;
	return (solve(beakerId + 1, rest));
}

int main() {

	while (cin >> N, N) {
		memset(Volume, 0, sizeof(Volume));
		memset(WaterIn, false, sizeof(WaterIn));
		for (int i = 0; i < N; ++i) {
			cin >> Volume[i];
		}
		sort(Volume, Volume + N);
		WaterIn[0] = true;
	
		if (solve(1, Volume[N - 1] - Volume[0])) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3020
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N;
int C[50];
bool g(const long,const long);
bool f(int res,int start,long now,long usd)
{
	if(res==0)
	{
		return g(now,usd);
	}
	int a=__builtin_ctzll(now);
	int b=__builtin_ctzll(~usd);
	if(a>b&&C[b]>res)return false;
	int pre=-1;
	for(int i=start;i<N;i++)
	{
		if(usd>>i&1||C[i]>res||pre==C[i])continue;
		if(f(res-C[i],i+1,now|1LL<<i,usd|1LL<<i))return true;
		pre=C[i];
	}
	return false;
}
bool g(long now,long usd)
{
	if(usd==((1LL<<N)-1))return true;
	for(int i=0;i<N;i++)
	{
		if(now>>i&1)
		{
			if(f(C[i],i+1,now&~(1LL<<i),usd))return true;
		}
	}
	return false;
}
main()
{
	while(cin>>N,N)
	{
		for(int i=0;i<N;i++)cin>>C[i];
		sort(C,C+N);
		reverse(C,C+N);
	    cout<<(g(1,1)?"YES":"NO")<<endl;
	}
}
accuracy: 1
time_ms: 840
memory_kb: 3016
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N;
int C[50];
bool g(const long,const long);
bool f(int res,int start,long now,long usd)
{
	if(res==0)
	{
		return g(now,usd);
	}
	int a=__builtin_ctz(now);
	int b=__builtin_ctz(~usd);
	if(a>b&&C[b]>res)return false;
	int pre=-1;
	for(int i=start;i<N;i++)
	{
		if(usd>>i&1||C[i]>res||pre==C[i])continue;
		if(f(res-C[i],i+1,now|1LL<<i,usd|1LL<<i))return true;
		pre=C[i];
	}
	return false;
}
bool g(long now,long usd)
{
	if(usd==((1LL<<N)-1))return true;
	for(int i=0;i<N;i++)
	{
		if(now>>i&1)
		{
			if(f(C[i],i+1,now&~(1LL<<i),usd))return true;
		}
	}
	return false;
}
main()
{
	while(cin>>N,N)
	{
		for(int i=0;i<N;i++)cin>>C[i];
		sort(C,C+N);
		reverse(C,C+N);
	    cout<<(g(1,1)?"YES":"NO")<<endl;
	}
}
accuracy: 1
time_ms: 830
memory_kb: 3112
final_rank: 20

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N;
int C[50];
bool g(const long,const long);
bool f(const int res,const int start,const long now,const long usd)
{
	if(res==0)
	{
		return g(now,usd);
	}
	int a=__builtin_ctz(now);
	int b=__builtin_ctz(~usd);
	if(a>b&&C[b]>res)return false;
	int pre=-1;
	for(int i=start;i<N;i++)
	{
		if(usd>>i&1||C[i]>res||pre==C[i])continue;
		if(f(res-C[i],i+1,now|1LL<<i,usd|1LL<<i))return true;
		pre=C[i];
	}
	return false;
}
bool g(const long now,const long usd)
{
	if(usd==((1LL<<N)-1))return true;
	for(int i=0;i<N;i++)
	{
		if(now>>i&1)
		{
			if(f(C[i],i+1,now&~(1LL<<i),usd))return true;
		}
	}
	return false;
}
main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
	while(cin>>N,N)
	{
		for(int i=0;i<N;i++)cin>>C[i];
		sort(C,C+N);
		reverse(C,C+N);
	    cout<<(g(1,1)?"YES\n":"NO\n");
	}
}
accuracy: 1
time_ms: 770
memory_kb: 2972
final_rank: 18

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 55

int N;
vector<int> V;
bool check[NUM];

bool recursive(int);


bool dfs(int water_index,int current_index,int water_rest){
	if(water_rest == 0)return recursive(water_index+1);
	if(current_index == N)return false;

	if(check[current_index] == false && V[current_index] <= water_rest){
		check[current_index] = true;
		if(dfs(water_index,current_index+1,water_rest-V[current_index]))return true;
		check[current_index] = false;
	}
	return dfs(water_index,current_index+1,water_rest);
}

bool recursive(int index){

	if(index == N)return true;

	if(!check[index])return false;

	return dfs(index,index,V[index]) || recursive(index+1);
}

void func(){

	V.clear();

	int tmp;

	for(int loop = 0; loop < N; loop++){

		scanf("%d",&tmp);
		V.push_back(tmp);
	}

	if(N == 1){
		printf("YES\n");
		return;
	}

	sort(V.rbegin(),V.rend());

	check[0] = true;
	for(int i = 1; i < N; i++){

		check[i] = false;
	}

	if(recursive(0)){

		printf("YES\n");
	}else{

		printf("NO\n");
	}
}


int main(){

	while(true){

		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3072
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

bool dfs(int i);
int n,a[50];
bool fil[50];

bool dfs2(int i,int j,int rem){
    if(rem==0) return dfs(i+1);
    if(j==n) return false;

    if(!fil[j] && a[j]<=rem){
        fil[j]=true;
        if(dfs2(i,j+1,rem-a[j])) return true;
        fil[j]=false;
    }
    return dfs2(i,j+1,rem);
}

bool dfs(int i){
    if(i==n) return true;
    if(!fil[i]) return false;
    return dfs2(i,i+1,a[i]) || dfs(i+1);
}

int main(){
    for(;scanf("%d",&n),n;){
        rep(i,n){
            scanf("%d",a+i);
            fil[i]=false;
        }
        sort(a,a+n,greater<int>());
        fil[0]=true;
        puts(dfs(0)?"YES":"NO");
    }

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3068
final_rank: 11

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"

bool getans(vector<int>&used, vector<int>&vs, int rest) {
	if (rest) {
		bool flag = false;
		for (int i = 0; i < used.size(); ++i) {
			if (used[i] == 0) {
				used[i] = 2;
				if (rest >= vs[i]) {

					if (getans(used, vs, rest - vs[i])) {
						used[i] = 0;
						return true;
					}
				}
				used[i] = 0;
				if (!flag)return false;
			}
			else if(used[i]==2){
				flag = true;
			}
		}
		return false;
	}
	else {
		if (all_of(used.begin(), used.end(), [](const int n) {return n; })) {
			return true;
		}
		bool flag = false;
		for (int i = 0; i < used.size(); ++i) {
			if (used[i] == 2) {
				used[i] = 1;
				bool ok=getans(used, vs, vs[i]);
				used[i] = 2;
				if (ok)return true;
			}
			else if (used[i] == 0) {
				return false;
			}
		}
		return false;
	}
}
int main() {
	while (1) {
		int N; cin >> N;
		if (!N)break;
		vector<int>vs(N),used(N);
		for (int i = 0; i < N; ++i) {
			cin >> vs[i];
		}
		sort(vs.begin(), vs.end(), greater<int>());
		used[0] = 2;
		bool ans = getans(used,vs, 0);
		if (ans)cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 3120
final_rank: 17

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int n;
int a[50];
bool b[50];

bool pourAll(int p);

bool pour(int p, int q, int rest) {
  if (!rest) {
    return pourAll(p+1);
  }
  if (q == n) {
    return false;
  }
  if (!b[q] && a[q] <= rest) {
    b[q] = true;
    if (pour(p, q+1, rest-a[q])) {
      return true;
    }
    b[q] = false;
  }
  return pour(p, q+1, rest);
}

bool pourAll(int p) {
  if (p == n) {
    return true;
  }
  if (!b[p]) {
    return false;
  }
  if (pour(p, p+1, a[p])) {
    return true;
  }
  return pourAll(p+1);
}

bool solve() {
  sort(a, a+n, greater<int>());
  b[0] = true;
  for (int i=1; i<n; i++) {
    b[i] = false;
  }

  return pourAll(0);
}

int main() {
  bool ans;
  while (1) {
    cin >> n;
    if (!n) {
      break;
    }
    for (int i=0; i<n; i++) {
      cin >> a[i];
    }
    ans = solve();
    cout << (ans ? "YES" : "NO") << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1172
final_rank: 1

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

int n;
bool ans;
vector<int> b;
void mainDFS(int x,vector<int> &c);

void subSolve(int x,int r,vector<int> &c,int X){
	//?????????
	if( ans ) return;
	
	//???????????§??\?????????
	if( x == n ){
		if( r == 0 ){
			mainDFS(X+1,c);
		}
		return;
	}
	//????????????????????????
	if( c[x] == 0 && b[x] <= r ){
		c[x] = b[x];
		subSolve(x+1,r-b[x],c,X);
		c[x] = 0;
	}
	//???????????????
	subSolve(x+1,r,c,X);
}

void mainDFS(int x,vector<int> &c){
	if( x == n ){ ans = 1; return; }
	//?????????
	if( ans ) return;
	if( c[x] == 0 ){ return; }
	else{
		// ?¬??????????????????????????????¶????????????
		if( x + 1 < n && c[x+1] == 0 ){
			c[x+1] = b[x+1];
			subSolve(x+2,c[x]-b[x+1],c,x);
			c[x+1] = 0;
		}else{
			//???????????????
			mainDFS(x+1,c);
			//??\????????????
			subSolve(x+1,c[x],c,x);
		}		
	}
}
int main(){
	while( cin >> n && n ){
		b = vector<int>(n);
		for(int i = 0 ; i < n ; i++) cin >> b[i];
		sort(b.rbegin(),b.rend());
		vector<int> s(n,0);
		s[0] = b[0];
		ans = 0;
		mainDFS(0,s);
		if(ans) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
}
accuracy: 1
time_ms: 240
memory_kb: 1192
final_rank: 9

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int n;
vint v;
vint tt;
vint used;
bool f=false;
int cnt=0;
int foo(int x);
int foo2(int c,int x,int y){
	if(c==0){
		return foo(x+1);
	}
	if(y>=n){
		return 0;
	}
	int ret=0;
	reep(i,y,n){
		if(v[i]<=c&&used[i]==0){
			used[i]=1;
			++cnt;
			if(foo2(c-v[i],x,i+1)&&cnt==n){
				return 1;
			}
			used[i]=0;
			--cnt;
		}
	}
	return 0;
}
int foo(int x){
	if(x==n){
		return 1;
	}
	if(used[x]==0) return 0;
	if(used[x]==2) return foo(x+1);
	int ret=0;
	used[x]=2;
	if(foo2(v[x],x,x+1)) return 1;
	used[x]=1;
	return foo(x+1);
}
void mainmain(){
	while(cin>>n,n){
		v=vint(n);
		rep(i,n){
			cin>>v[i];
		}
		sort(ALL(v));
		reverse(ALL(v));
		tt=vint(n,0);
		used=vint(n,0);
		// rep(i,n) cout<<used[i]<<" ";
		// cout<<endl;
		used[0]=1;
		tt[0]=v[0];
		cnt=1;
		if(foo(0)) cout<<"YES\n";
		else cout<<"NO\n";
	}
}



}
main() try{
    mainmain();
}
catch(...){
}
accuracy: 1
time_ms: 60
memory_kb: 1180
final_rank: 2

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int n;
vint v;
vint tt;
vint used;
bool f=false;
int cnt=0;
int foo(int x);
int foo2(int c,int x,int y){
	if(c==0){
		return foo(x+1);
	}
	if(y>=n){
		return 0;
	}
	int ret=0;
	if(v[y]<=c&&used[y]==0){
		used[y]=1;
		++cnt;
		if(foo2(c-v[y],x,y+1)&&cnt==n){
			return 1;
		}
		used[y]=0;
		--cnt;
	}
	return foo2(c,x,y+1);
}
int foo(int x){
	if(x==n){
		return 1;
	}
	if(used[x]==0) return 0;
	if(used[x]==2) return foo(x+1);
	int ret=0;
	used[x]=2;
	if(foo2(v[x],x,x+1)) return 1;
	used[x]=1;
	return foo(x+1);
}
void mainmain(){
	while(cin>>n,n){
		v=vint(n);
		rep(i,n){
			cin>>v[i];
		}
		sort(ALL(v));
		reverse(ALL(v));
		tt=vint(n,0);
		used=vint(n,0);
		// rep(i,n) cout<<used[i]<<" ";
		// cout<<endl;
		used[0]=1;
		tt[0]=v[0];
		cnt=1;
		if(foo(0)) cout<<"YES\n";
		else cout<<"NO\n";
	}
}



}
main() try{
    mainmain();
}
catch(...){
}
accuracy: 1
time_ms: 60
memory_kb: 1180
final_rank: 2

code_snippet: #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;
int n;
vint v;
vint tt;
vint used;
bool f=false;
int cnt=0;
int foo(int x);
int foo2(int c,int x,int y){
	if(c==0){
		return foo(x+1);
	}
	if(y>=n){
		return 0;
	}
	int ret=0;
	if(v[y]<=c&&used[y]==0){
		used[y]=1;
		cnt++;
		if(foo2(c-v[y],x,y+1)&&cnt==n){
			return 1;
		}
		used[y]=0;
		cnt--;
	}
	// reep(i,x+1,n){
		// if(used[i]==0&&c-v[i]>=0){
			// used[i]=1;
			// ret=max(ret,foo2(c-v[i],x));
			// if(ret) return ret;
			// used[i]=0;
		// }
	// }
	return foo2(c,x,y+1);
}
int foo(int x){
	// cout<<"x "<<x<<endl;
	// rep(i,n){
		// cout<<used[i]<<" ";
	// }
	// cout<<endl;
	if(x==n&&used[x-1]!=0){
		// cout<<"**************\n";
		// f=true;
		return 1;
	}
	if(used[x]==0) return 0;
	if(used[x]==2) return foo(x+1);
	int ret=0;
	used[x]=2;
	ret=max(ret,foo2(v[x],x,x+1));
	if(ret) return ret;
	used[x]=1;
	ret=max(ret,foo(x+1));
	return ret;
}
void mainmain(){
	while(cin>>n,n){
		v=vint(n);
		rep(i,n){
			cin>>v[i];
		}
		sort(ALL(v));
		reverse(ALL(v));
		tt=vint(n,0);
		used=vint(n,0);
		// rep(i,n) cout<<used[i]<<" ";
		// cout<<endl;
		used[0]=1;
		tt[0]=v[0];
		cnt=1;
		if(foo(0)) cout<<"YES\n";
		else cout<<"NO\n";
	}
}



}
main() try{
    mainmain();
}
catch(...){
}
accuracy: 1
time_ms: 60
memory_kb: 1184
final_rank: 4

code_snippet: #include<stdio.h>
#include<algorithm>
using namespace std;
int c[60];
int v[60];
int u[60];
int n;
int solve(int a,int b){
	if(b==0){
		bool ok=true;
		for(int i=0;i<n;i++)if(!u[i]){ok=false;break;}
		if(ok)return 1;
		int at=0;
		for(int i=0;i<n;i++){
			if(v[i]){
				v[i]=0;
				int res=solve(0,c[i]);
				if(res)return 1;
				v[i]=1;
			}
			if(!u[i])break;
		}
		return 0;
	}
	if(a==n)return 0;
	if(!u[a]&&b>=c[a]){
		u[a]=v[a]=1;
		if(solve(a+1,b-c[a]))return 1;
		u[a]=v[a]=0;
	}
	int rem=0;
	for(int i=a+1;i<n;i++)if(b>=c[i]&&!u[i])rem+=c[i];
	if(rem<b)return 0;
	return solve(a+1,b);
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		n=a;
		for(int i=0;i<a;i++)scanf("%d",c+i);
		for(int i=0;i<a;i++)c[i]=-c[i];
		std::sort(c,c+a);
		for(int i=0;i<a;i++)c[i]=-c[i];
		for(int i=0;i<a;i++)v[i]=u[i]=0;
		v[0]=u[0]=1;
		if(solve(0,0))printf("YES\n");
		else printf("NO\n");
	}
}
accuracy: 1
time_ms: 160
memory_kb: 1016
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;

int n;
int beaker[128];
bool filled[128];
bool used[128];

bool check();

bool pour(int vol, int indx) {
	if(vol == 0)
		return true;
	if(vol < 0)
		return false;
	if(indx >= n)
		return false;

	if(!filled[indx] && !used[indx] && beaker[indx] <= vol) {
		filled[indx] = used[indx] = true;
		if(pour(vol - beaker[indx], indx + 1) && check())
			return true;
		filled[indx] = used[indx] = false;
	}

	return pour(vol, indx + 1);
}

bool check() {
	for(int i = 0; i < n; i++) {
		if(filled[i]) {
			filled[i] = false;
			if(pour(beaker[i], i + 1))
				return true;
			filled[i] = true;
		}
		else if(!used[i]) {
			return false;
		}
	}
	return true;
}

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

		sort(beaker, beaker + n, greater<int>());
		for(int i = 0; i < 128; i++) {
			filled[i] = used[i] = false;
		}
		filled[0] = used[0] = true;

		if(check())
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
}
accuracy: 1
time_ms: 100
memory_kb: 1172
final_rank: 6

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

const int N = 50;

int n;
vector<int> beaker;
bitset<N> full;
bitset<N> used;

bool pour(int water, int start);
bool check();

bool pour(int water, int start) {
    for(int i = start; i < n; ++i) {
        if(used[i]) continue;

        if(water < beaker[i]) continue;
        else if(water == beaker[i]) {
            full.set(i);
            used.set(i);
            if(check()) return true;
            full.reset(i);
            used.reset(i);
        } else {
            full.set(i);
            used.set(i);
            if(pour(water - beaker[i], i + 1)) return true;
            full.reset(i);
            used.reset(i);
        }
    }
    return false;
}

bool check() {
    if(used.count() == n) return true;
    for(int i = 0; i < n; ++i) {
        if(!used[i] && !used[i]) return false;
        if(!full[i]) continue;
        full.reset(i);
        if(pour(beaker[i], i + 1)) return true;
        full.set(i);
    }
    return false;
}

int main() {
    while(cin >> n, n) {
        beaker.resize(n);
        for(auto& b: beaker) cin >> b;
        sort(beaker.begin(), beaker.end(), greater<int>());
        full = bitset<N>(1);
        used = bitset<N>(1);
        cout << (check() ? "YES" : "NO") << endl;
    }
}
accuracy: 1
time_ms: 100
memory_kb: 1208
final_rank: 8

code_snippet: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ ";if(_c.size()){cout<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;}cout<<" ]"<<endl; }
}
ostream &operator<<(ostream &os, const pair<int,int> &p){return(os<<"("<<p.first<<","<<p.second<<")");}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

int n;
int b[101], f[101];
enum { EMPTY=0,FULL=1,USED=2 };

void dbg(){
    pf("f = "); REP(i,n) pf("%2d, ", f[i]); pf("\n");
}

bool move(int i, int v, int n_empty);
bool rec(int n_empty){
    if(n_empty == 0) return true;
    REP(i,n){
        if(f[i] == EMPTY) break;
        if(f[i] == FULL){
            f[i] = USED; dbg();
            if(move(i+1, b[i], n_empty)) return true;
            f[i] = FULL; dbg();
        }
    }
    return false;
}
bool move(int i, int v, int n_empty){
    if(v == 0) return rec(n_empty);
    FROMTO(j, i, n){
        if(f[j] != EMPTY) continue;
        if(v < b[j]) continue;
        f[j] = FULL; dbg();
        if(move(j+1, v-b[j], n_empty-1)) return true;
        if(i == j && f[j] == EMPTY) break;
        f[j] = EMPTY; dbg();
    }
    return false;
}

bool solve(){
    sort(b, b+n, greater<int>());
    pf("b = "); REP(i,n) pf("%2d, ", b[i]); pf("\n");
    REP(i,n) f[i] = EMPTY;
    f[0] = FULL; dbg();
    return rec(n-1);
}

int main(){
    while(cin >> n && n){
        REP(i,n) cin >> b[i];
        cout << (solve()?"YES":"NO") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 1176
final_rank: 5