Problem:
Problem D:
Divisor is the Conqueror
Divisor is the Conquerer
is a solitaire card game. Although this simple game itself is a great
way to pass one’s time, you, a programmer, always kill your time by programming. Your task
is to write a computer program that automatically solves
Divisor is the Conquerer
games. Here
is the rule of this game:
First, you randomly draw
N
cards (1 ≤
N
≤ 52) from your deck of playing cards. The game is
played only with those cards; the other cards will not be used.
Then you repeatedly pick one card to play among those in your hand. You are allowed to choose
any card in the initial turn. After that, you are only allowed to pick a card that conquers the
cards you have already played. A card is said to conquer a set of cards when the rank of the card
divides the sum of the ranks in the set. For example, the card 7 conquers the set {5, 11, 12},
while the card 11 does not conquer the set {5, 7, 12}.
You win the game if you successfully play all
N
cards you have drawn. You lose if you fails,
that is, you faces a situation in which no card in your hand conquers the set of cards you have
played.
Input
The input consists of multiple test cases.
Each test case consists of two lines. The first line contains an integer
N
(1 ≤ N ≤ 52), which represents the number of the cards. The second line contains
N
integers
c
1
, . . . , c
N
(1 ≤
c
i
≤ 13),
each of which represents the rank of the card. You may assume that there are at most four cards
with the same rank in one list.
The input is terminated by a line with a single zero.
Output
For each test case, you should output one line. If there are any winning strategies for the cards
of the input, print any one of them as a list of
N
integers separated by a space.
If there is no winning strategy, print “No”.
Sample Input
5
1 2 3 3 7
4
2 3 3 3
0
Output for the Sample Input
3 3 1 7 2
No

Submitted solutions:

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

typedef long long ll;
vector<int> cur;

int cnt[14];
bool rec(int n, int sum)
{
    if(n == 0)
        return 1;

    for(int i = 1; i <= 13; i++){
        if(cnt[i] != 0 && sum % i == 0){
            cur.push_back(i);

            cnt[i]--;
            if(rec(n - 1, sum - i))
                return 1;

            cnt[i]++;
            cur.pop_back();
        }
    }
    return 0;
}

int main() {
    while (1) {
        int n;
        cin >> n;

        if (n == 0)
            exit(0);

        int sum = 0;

        for (int i = 0; i < 14; i++)
            cnt[i] = 0;

        for(int i = 0; i < n; i++){
            int x;
            cin >> x;

            cnt[x]++;

            sum += x;
        }

        cur.clear();
        if(rec(n, sum)) {
            for (int i = n - 1; i > 0; i--)
                cout << cur[i] << " ";
            cout << cur[0] << endl;
        } else
            cout << "No" << endl;
    }
}
accuracy: 1
time_ms: 450
memory_kb: 3296
final_rank: 9

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    while (cin >> n, n) {
        vector<int> c(13);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int a;
            cin >> a;
            sum += a;
            c[a - 1] += 1;
        }
        vector<int> res;
        set<vector<int>> used;
        auto slv = [&](auto self, int rem) -> bool {
            if (rem == 0) return true;
            if (used.find(c) != used.end()) return false;
            used.insert(c);
            for (int i = 0; i < 13; i++) {
                if (c[i] and rem % (i + 1) == 0) {
                    c[i] -= 1;
                    res.push_back(i + 1);
                    if (self(self, rem - (i + 1))) return true;
                    res.pop_back();
                    c[i] += 1;
                }
            }
            return false;
        };
        if (slv(slv, sum)) {
            reverse(res.begin(), res.end());
            for (int i = 0; i < res.size(); i++) {
                if (i) cout << " ";
                cout << res[i];
            }
            cout << "\n";
        } else {
            cout << "No\n";
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3980
final_rank: 14

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

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

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vvl = vector<vector<ll>>;
using vvi = vector<vector<int>>;
using vvpll = vector<vector<pll>>;
#define rep(i, a, b) for (ll i=(a); i<(b); i++)
#define rrep(i, a, b) for (ll i=(a); i>(b); i--)
#define pb push_back
#define tostr to_string
#define ALL(A) A.begin(), A.end()
// constexpr ll INF = LONG_LONG_MAX;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;

const string digits = "0123456789";
const string ascii_lowercase = "abcdefghijklmnopqrstuvwxyz";
const string ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string ascii_letters = ascii_lowercase + ascii_uppercase;

template<typename T> vector<vector<T>> list2d(int N, int M, T init) { return vector<vector<T>>(N, vector<T>(M, init)); }
template<typename T> vector<vector<vector<T>>> list3d(int N, int M, int L, T init) { return vector<vector<vector<T>>>(N, vector<vector<T>>(M, vector<T>(L, init))); }
template<typename T> vector<vector<vector<vector<T>>>> list4d(int N, int M, int L, int O, T init) { return vector<vector<vector<vector<T>>>>(N, vector<vector<vector<T>>>(M, vector<vector<T>>(L, vector<T>(O, init)))); }

void print(ld out) { cout << fixed << setprecision(15) << out << '\n'; }
void print(double out) { cout << fixed << setprecision(15) << out << '\n'; }
template<typename T> void print(T out) { cout << out << '\n'; }
template<typename T1, typename T2> void print(pair<T1, T2> out) { cout << out.first << ' ' << out.second << '\n'; }
template<typename T> void print(vector<T> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size()-1 ? '\n' : ' '); } }
template<typename T> void print(set<T> S) { vector<T> A(S.begin(), S.end()); print(A); }

void Yes() { print("Yes"); }
void No() { print("No"); }
void YES() { print("YES"); }
void NO() { print("NO"); }

ll floor(ll a, ll b) { if (a < 0) { return (a-b+1) / b; } else { return a / b; } }
ll ceil(ll a, ll b) { if (a >= 0) { return (a+b-1) / b; } else { return a / b; } }
pll divmod(ll a, ll b) { ll d = a / b; ll m = a % b; return {d, m}; }
template<typename T> bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; }
template<typename T> bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; }

template<typename T> T sum(vector<T> A) { T res = 0; for (T a: A) res += a; return res; }
template<typename T> T max(vector<T> A) { return *max_element(ALL(A)); }
template<typename T> T min(vector<T> A) { return *min_element(ALL(A)); }

ll toint(string s) { ll res = 0; for (char c : s) { res *= 10; res += (c - '0'); } return res; }
int toint(char num) { return num - '0'; }
char tochar(int num) { return '0' + num; }
int ord(char c) { return (int)c; }
char chr(int a) { return (char)a; }

ll pow(int x, ll n) { ll res = 1; rep(_, 0, n) res *= x; return res; }
ll pow(ll x, ll n, int mod) { ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; }

int popcount(ll S) { return __builtin_popcountll(S); }
ll gcd(ll a, ll b) { return __gcd(a, b); }

template<typename T> int bisect_left(vector<T> &A, T val) { return lower_bound(ALL(A), val) - A.begin(); }
template<typename T> int bisect_right(vector<T> &A, T val) { return upper_bound(ALL(A), val) - A.begin(); }

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

    while (1) {
        ll N;
        cin >> N;
        if (!N) return 0;

        vector<ll> A(N);
        rep(i, 0, N) cin >> A[i];

        vector<ll> C(14);
        rep(i, 0, N) C[A[i]]++;
        vector<ll> ans;
        auto rec = [&](auto&& f, ll cnt, ll sm) {
            if (cnt == N) {
                return true;
            }
            rep(k, 1, 14) {
                if (C[k] and (sm-k) % k == 0) {
                    C[k]--;
                    if (f(f, cnt+1, sm-k)) {
                        ans.pb(k);
                        return true;
                    }
                    C[k]++;
                }
            }
            return false;
        };
        ll total = sum(A);
        rep(k, 1, 14) {
            if (C[k] and (total-k) % k == 0) {
                C[k]--;
                if (rec(rec, 1, total-k)) {
                    ans.pb(k);
                    break;
                }
                C[k]++;
            }
        }
        if (!ans.empty()) {
            print(ans);
        } else {
            No();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 3168
final_rank: 12

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

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

using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vvl = vector<vector<ll>>;
using vvi = vector<vector<int>>;
using vvpll = vector<vector<pll>>;
#define rep(i, a, b) for (ll i=(a); i<(b); i++)
#define rrep(i, a, b) for (ll i=(a); i>(b); i--)
#define pb push_back
#define tostr to_string
#define ALL(A) A.begin(), A.end()
// constexpr ll INF = LONG_LONG_MAX;
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;

const string digits = "0123456789";
const string ascii_lowercase = "abcdefghijklmnopqrstuvwxyz";
const string ascii_uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string ascii_letters = ascii_lowercase + ascii_uppercase;

template<typename T> vector<vector<T>> list2d(int N, int M, T init) { return vector<vector<T>>(N, vector<T>(M, init)); }
template<typename T> vector<vector<vector<T>>> list3d(int N, int M, int L, T init) { return vector<vector<vector<T>>>(N, vector<vector<T>>(M, vector<T>(L, init))); }
template<typename T> vector<vector<vector<vector<T>>>> list4d(int N, int M, int L, int O, T init) { return vector<vector<vector<vector<T>>>>(N, vector<vector<vector<T>>>(M, vector<vector<T>>(L, vector<T>(O, init)))); }

void print(ld out) { cout << fixed << setprecision(15) << out << '\n'; }
void print(double out) { cout << fixed << setprecision(15) << out << '\n'; }
template<typename T> void print(T out) { cout << out << '\n'; }
template<typename T1, typename T2> void print(pair<T1, T2> out) { cout << out.first << ' ' << out.second << '\n'; }
template<typename T> void print(vector<T> A) { rep(i, 0, A.size()) { cout << A[i]; cout << (i == A.size()-1 ? '\n' : ' '); } }
template<typename T> void print(set<T> S) { vector<T> A(S.begin(), S.end()); print(A); }

void Yes() { print("Yes"); }
void No() { print("No"); }
void YES() { print("YES"); }
void NO() { print("NO"); }

ll floor(ll a, ll b) { if (a < 0) { return (a-b+1) / b; } else { return a / b; } }
ll ceil(ll a, ll b) { if (a >= 0) { return (a+b-1) / b; } else { return a / b; } }
pll divmod(ll a, ll b) { ll d = a / b; ll m = a % b; return {d, m}; }
template<typename T> bool chmax(T &x, T y) { return (y > x) ? x = y, true : false; }
template<typename T> bool chmin(T &x, T y) { return (y < x) ? x = y, true : false; }

template<typename T> T sum(vector<T> A) { T res = 0; for (T a: A) res += a; return res; }
template<typename T> T max(vector<T> A) { return *max_element(ALL(A)); }
template<typename T> T min(vector<T> A) { return *min_element(ALL(A)); }

ll toint(string s) { ll res = 0; for (char c : s) { res *= 10; res += (c - '0'); } return res; }
int toint(char num) { return num - '0'; }
char tochar(int num) { return '0' + num; }
int ord(char c) { return (int)c; }
char chr(int a) { return (char)a; }

ll pow(int x, ll n) { ll res = 1; rep(_, 0, n) res *= x; return res; }
ll pow(ll x, ll n, int mod) { ll res = 1; while (n > 0) { if (n & 1) { res = (res * x) % mod; } x = (x * x) % mod; n >>= 1; } return res; }

int popcount(ll S) { return __builtin_popcountll(S); }
ll gcd(ll a, ll b) { return __gcd(a, b); }

template<typename T> int bisect_left(vector<T> &A, T val) { return lower_bound(ALL(A), val) - A.begin(); }
template<typename T> int bisect_right(vector<T> &A, T val) { return upper_bound(ALL(A), val) - A.begin(); }

template<typename T>
unordered_map<T, ll> Counter(vector<T> &A) {
    unordered_map<T, ll> res;
    for (T a : A) {
        res[a]++;
    }
    return res;
}

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

    while (1) {
        ll N;
        cin >> N;
        if (!N) return 0;

        vector<ll> A(N);
        rep(i, 0, N) cin >> A[i];

        auto C = Counter(A);
        vector<ll> ans;
        auto rec = [&](auto&& f, ll cnt, ll sm) {
            if (cnt == N) {
                return true;
            }
            rep(k, 1, 14) {
                if (C[k] and (sm-k) % k == 0) {
                    C[k]--;
                    if (f(f, cnt+1, sm-k)) {
                        ans.pb(k);
                        return true;
                    }
                    C[k]++;
                }
            }
            return false;
        };
        ll total = sum(A);
        rep(k, 1, 14) {
            if (C[k] and (total-k) % k == 0) {
                C[k]--;
                if (rec(rec, 1, total-k)) {
                    ans.pb(k);
                    break;
                }
                C[k]++;
            }
        }
        if (!ans.empty()) {
            print(ans);
        } else {
            No();
        }
    }
    return 0;
}
accuracy: 1
time_ms: 3070
memory_kb: 3488
final_rank: 20

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

set<long long int>aset;
long long int ahash(vector<int>&v) {
	long long int k=0;
	for (auto a : v) {
		k*=5;
		k+=a;
	}
	return k;
}
vector<int> solve(vector<int>&v,int rest) {
	if (rest == 0)return vector<int>{-1};
	auto ha=ahash(v);
	if (aset.find(ha) == aset.end()) {
		//aset.emplace(ha);
	}
	else {
		return vector<int>();
	}
	for (int i = 0; i < 13; ++i) {
		if (rest % (i + 1)==0 && v[i]) {
			v[i]--;
			auto k=solve(v, rest-i-1);
			if(!k.empty()){
				k.push_back(i+1);
				return k;
			}
			v[i]++;
		}
	}
	return vector<int>();
}

int main() {
	while (true) {
		aset.clear();
		int N;cin>>N;
		if(!N)break;
		int sum=0;
		vector<int>nums(13);
		for (int i = 0; i < N; ++i) {
			int a;cin>>a;a--;
			sum+=a+1;
			nums[a]++;
		}
		vector<int> oks=solve(nums,sum);
		if (!oks.empty()) {
			for (int i = 0; i < N; ++i) {
				cout<<oks[i+1];
				if(i==N-1)cout<<endl;
				else cout<<" ";
			}
		}
		else {
			cout<<"No"<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 980
memory_kb: 3028
final_rank: 10

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

set<long long int>aset;
long long int ahash(vector<int>&v) {
	long long int k=0;
	for (auto a : v) {
		k*=5;
		k+=a;
	}
	return k;
}
vector<int> solve(vector<int>&v,int rest) {
	if (rest == 0)return vector<int>{-1};
	auto ha=ahash(v);
	if (aset.find(ha) == aset.end()) {
		aset.emplace(ha);
	}
	else {
		return vector<int>();
	}
	for (int i = 0; i < 13; ++i) {
		if (rest % (i + 1)==0 && v[i]) {
			v[i]--;
			auto k=solve(v, rest-i-1);
			if(!k.empty()){
				k.push_back(i+1);
				return k;
			}
			v[i]++;
		}
	}
	return vector<int>();
}

int main() {
	while (true) {
		aset.clear();
		int N;cin>>N;
		if(!N)break;
		int sum=0;
		vector<int>nums(13);
		for (int i = 0; i < N; ++i) {
			int a;cin>>a;a--;
			sum+=a+1;
			nums[a]++;
		}
		vector<int> oks=solve(nums,sum);
		if (!oks.empty()) {
			for (int i = 0; i < N; ++i) {
				cout<<oks[i+1];
				if(i==N-1)cout<<endl;
				else cout<<" ";
			}
		}
		else {
			cout<<"No"<<endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3288
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



int N;
bool FLG;

void recursive(int cards[14],int log[],int used_num,int sum){

	if(FLG)return;

	if(used_num == N){

		FLG = true;

		reverse(log,log+N);

		printf("%d",log[0]);
		for(int i = 1; i < N; i++){
			printf(" %d",log[i]);
		}
		printf("\n");

		return;
	}

	for(int i = 1; i <= 13; i++){
		if(cards[i] > 0 && (used_num == N-1 || (sum-i)%i == 0)){
			int next_cards[14],next_log[N];
			for(int k = 1; k <= 13; k++){
				next_cards[k] = cards[k];
			}
			next_cards[i]--;
			for(int k = 0; k < used_num; k++){
				next_log[k] = log[k];
			}
			next_log[used_num] = i;

			recursive(next_cards,next_log,used_num+1,sum-i);
		}
	}
}


void func(){

	FLG = false;

	int first[14];
	for(int i = 1; i <= 13; i++){
		first[i] = 0;
	}

	int tmp,sum = 0;
	for(int i = 0; i < N; i++){
		scanf("%d",&tmp);
		first[tmp]++;
		sum += tmp;
	}

	int log[N];

	recursive(first,log,0,sum);


	if(!FLG){
		printf("No\n");
	}
}

int main(){

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

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 830
memory_kb: 3184
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int a[14],n,t;
int dfs(int s,int x){
  if(x==s)return 1;
  r(i,14)if(a[i]&&(s-x-i)%i==0){
    a[i]--;
    if(dfs(s,x+i)){
      if(x+i!=s)cout<<' ';
      cout<<i;
      return 1;
    }
    a[i]++;
  }
  return 0;
}
main(){
  while(cin>>n,n){
    int sum=0;
    r(i,14)a[i]=0;
    r(i,n){
      cin>>t;
      a[t]++;
      sum+=t;
    }
    if(!dfs(sum,0))cout<<"No";
    cout<<endl;
  }
}
accuracy: 1
time_ms: 550
memory_kb: 2924
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
unordered_map<int,bool> mp;
int n,a[14],ans[52],c;

int cal(){
  int res=0;
  for(int i=1;i<=13;i++)res=res*5+a[i];
  return res;
}

bool dfs(int s,int d){
  if(!d)return 1;
  int y=cal();
  if(mp.count(y))return mp[y];
  for(int i=13;i>0;i--){
    if(a[i]&&(s-i)%i==0){
      ans[d-1]=i;
      a[i]--;
      if(dfs(s-i,d-1))return mp[y]=1;
      a[i]++;
    }
  }
  return 0;
}

int main(){
  while(cin>>n,n){
    memset(a,0,sizeof(a));
    mp.clear();
    c=0;
    for(int i=0,b;i<n;i++)scanf("%d",&b),a[b]++,c+=b;
    if(dfs(c,n)){
      for(int i=0;i<n;i++){
	if(i)printf(" ");
	printf("%d",ans[i]);
      }
    }else printf("No");
    printf("\n");
  }
  return 0;
}
accuracy: 1
time_ms: 2240
memory_kb: 3136
final_rank: 19

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

int a[14],b[55],n,f;
void dfs(int s,int k){
  if(f)return;
  if(!s) {
    f=1;
    for(int i=n-1;i>=0;i--) {
      if(i<n-1)cout<<" ";
      cout<<b[i];
    }
    cout<<endl;
    return;
  }
  for(int i=1;i<14;i++) {
    if(a[i]&&(s-i)%i==0) {
      a[i]--;
      b[k]=i;
      dfs(s-i,k+1);
      a[i]++;
    }
  }
}
int main() {
  while(cin>>n&&n){
    memset(a,0,sizeof(a));
    int s=0;
    for(int i=0,x;i<n;i++){
      cin>>x;
      a[x]++;
      s+=x;
    }
    f=0;
    dfs(s,0);
    if(!f)cout<<"No"<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 390
memory_kb: 3100
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
unordered_map<int,bool> mp;
int n,a[14],ans[52],c;

int cal(){
  int res=0;
  for(int i=1;i<13;i++)res=res*5+a[i];
  return res;
}

bool dfs(int s,int d){
  if(!d)return 1;
  int y=cal();
  if(mp.count(y))return mp[y];
  for(int i=13;i>0;i--){
    if(a[i]&&(s-i)%i==0){
      ans[d-1]=i;
      a[i]--;
      if(dfs(s-i,d-1))return mp[y]=1;
      a[i]++;
    }
  }
  return 0;
}

int main(){
  while(cin>>n,n){
    memset(a,0,sizeof(a));
    mp.clear();
    c=0;
    for(int i=0,b;i<n;i++)scanf("%d",&b),a[b]++,c+=b;
    if(dfs(c,n)){
      for(int i=0;i<n;i++){
	if(i)printf(" ");
	printf("%d",ans[i]);
      }
    }else printf("No");
    printf("\n");
  }
  return 0;
}
accuracy: 1
time_ms: 2150
memory_kb: 3064
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
unordered_map<int,bool> mp;
int n,a[14],ans[52],c;

int cal(){
  int res=0;
  for(int i=1;i<13;i++)res=res*5+a[i];
  return res;
}

bool dfs(int s,int d){
  if(d==n)return 1;
  int y=cal();
  if(mp.count(y))return mp[y];
  for(int i=13;i>0;i--){
    if(a[i]&&(s-i)%i==0){
      ans[d]=i;
      a[i]--;
      if(dfs(s-i,d+1))return mp[y]=1;
      a[i]++;
    }
  }
  return 0;
}

int main(){
  while(cin>>n,n){
    memset(a,0,sizeof(a));
    mp.clear();
    c=0;
    for(int i=0,b;i<n;i++)scanf("%d",&b),a[b]++,c+=b;
    if(dfs(c,0)){
      for(int i=n-1;i>=0;i--){
	if(i!=n-1)printf(" ");
	printf("%d",ans[i]);
      }
    }else printf("No");
    printf("\n");
  }
  return 0;
}
accuracy: 1
time_ms: 2160
memory_kb: 3160
final_rank: 18

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

int N;

int card[14];
vector<int> res;

bool isvalid( int n,int i){
  if( (n-i) % i == 0 ) return true;
  return false;
}

bool solve(int n,int cnt){
  if( cnt == 1 ) {
    res.emplace_back( n );
    return true;
  }
  for(int i=13;i>=1;i--){
    if( card[i] == 0 ) continue;
    if( !isvalid(n,i) ) continue;
    card[i]--;
    res.emplace_back( i );
    if( solve( n - i, cnt-1 ) ) return true;
    res.pop_back();
    card[i]++;
  }
  return false;
}

int main(){
  while( cin >> N && N){
    memset( card,0,sizeof(card) );
    res.clear();
    int sum = 0;
    for(int i=0;i<N;i++){
      int a; cin >> a;
      sum += a;
      card[a]++;
    }    
    if( !solve(sum,N) )
      cout << "No" << endl;
    else {
      reverse( res.begin(), res.end() );
      for( int i=0;i<res.size();i++){
        if( i ) cout << " ";
        cout << res[i];
      }
      cout << endl;
    }      
  }
}
accuracy: 1
time_ms: 1830
memory_kb: 3032
final_rank: 16

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
vi out,in;
int n;
bool f(int a,int t){
	if(a==0){
		rep(i,n){
			if(i)cout<<" ";
			cout<<out[n-1-i];
		}
		cout<<endl;
		return true;
	}
	loop(i,1,14)if(in[i]&&(a-i)%i==0){
		in[i]--;
		out[t]=i;
		if(f(a-i,t+1))return true;
		in[i]++;
	}
	return false;
}
int main(){
	while(cin>>n,n){
		out=vi(n);
		in=vi(14);
		int sum=0;
		rep(i,n){
			int a;cin>>a;
			sum+=a;
			in[a]++;
		}
		if(!f(sum,0))cout<<"No"<<endl;
	}
}
accuracy: 1
time_ms: 700
memory_kb: 1216
final_rank: 5

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

using namespace std;

const int cards = 13;
 
bool flg;
 
void rec(vector<int> a,int sum,vector<int> &v){
    if(sum == 0){
	flg = true;
	return;
    }
    for(int i = 1 ; i <= 13 ; i++){
	if(a[i] && sum % i == 0){
	    v.push_back(i);
	    a[i]--;
	    rec(a,sum-i,v);
	    if(flg) return;
	    v.pop_back();
	    a[i]++;
	}
    }
}
 
int main(){
    int n;
 
    while(cin >> n ,n){
	vector<int> a(cards+1,0),v;
	int m,sum = 0;
 
	for(int i = 0 ; i < n ; i++){
	    cin >> m;
 
	    sum += m;
	    a[m]++;
	}
 
	flg = false;
	rec(a,sum,v);
 
	if(flg){
	    int len = (int)v.size();
	    reverse(v.begin(),v.end());
	    for(int i = 0 ; i < len ; i++){
		if(!i) cout << v[i];
		else cout << " " << v[i];
	    }
	    cout << endl;
	}
	else cout << "No" << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 1120
memory_kb: 3056
final_rank: 13

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 52;
const int MAX_C = 13;

/* typedef */

/* global variables */

int n;
int cs[MAX_C + 1], ans[MAX_N];

/* subroutines */

bool rec(int k, int sum) {
  if (k == n - 1) {
    ans[k] = sum;
    return true;
  }
  
  for (int i = 1; i <= MAX_C; i++)
    if (cs[i] > 0 && sum % i == 0) {
      cs[i]--;
      ans[k] = i;
      if (rec(k + 1, sum - i)) return true;
      cs[i]++;
    }

  return false;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0) break;

    int sum = 0;
    memset(cs, 0, sizeof(cs));

    for (int i = 0; i < n; i++) {
      int ci;
      cin >> ci;
      cs[ci]++;
      sum += ci;
    }
    //cout << sum << endl;

    if (rec(0, sum)) {
      for (int i = n - 1; i >= 0; i--) {
	if (i < n - 1) cout << ' ';
	cout << ans[i];
      }
      cout << endl;
    }
    else
      cout << "No" << endl;
  }
  
  return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 1160
final_rank: 2

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;
 
static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};
 
static const double EPS = 1e-8;

vector<char> answer;

bool dfs(int sum,vector<char>& cards,vector<char>& order){
  if(sum == 0){
    reverse(order.begin(),order.end());
    answer = order;
    return true;
  }
  bool res = false;

  for(int i = 1; i <= 13; i++){
    if(cards[i] > 0 && (sum - i) % i == 0){
      cards[i]--;
      order.push_back(i);
      res |= dfs(sum - i,cards,order);
      if(res) return true;
      order.pop_back();
      cards[i]++;
    }
  }
  return res;
}

int main(){
  int num_of_cards;
  while(~scanf("%d",&num_of_cards)){
    if(num_of_cards == 0) break;

    vector<char> cards(14);
    int total_rank = 0;
    for(int i = 0; i < num_of_cards; i++){
      int rank;
      scanf("%d",&rank);
      cards[rank]++;
      total_rank += rank;
    }
    vector<char> order;
    bool isok = dfs(total_rank,cards,order);
    bool is_first = true;

    if(isok){
      for(int i = 0; i < answer.size(); i++){
        printf("%s%d",is_first ? "" : " ",answer[i]);
        is_first = false;
      }
      printf("\n");
    }
    else{
      printf("No\n");
    }
  }
}
accuracy: 1
time_ms: 670
memory_kb: 1228
final_rank: 4

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

inline ll encode(const vector<int> &a){
  ll res = 0;
  for(int i=13;i>=1;i--)res = res*5 + a[i];
  return res;
}

set<int> memo;
vector<int> ans;

bool rec(ll sum, vector<int> &rem){
  int a = encode(rem);
  if(a==0)return true;
  if(memo.count(a))return false;
  memo.insert(a);

  for(int i=13;i>=1;i--){
    if(rem[i] && sum%i==0){
      rem[i]--;
      ans.push_back(i);
      if(rec(sum - i, rem))return true;
      ans.pop_back();
      rem[i]++;
    }
  }
  return false;
}

int main(){
  int n;
  while(cin >> n, n){
    vector<int> num(14,0);
    int sum = 0;
    for(int i=0;i<n;i++){
      int tmp;
      cin >> tmp;
      num[tmp]++; sum+=tmp;
    }

    memo.clear();
    ans.clear();
    rec(sum,num);
    if((int)ans.size() == n){
      reverse(ans.begin(),ans.end());
      for(int i=0;i<n;i++){
	cout << ans[i] << (i==n-1?"\n":" ");
      }
    }else cout << "No" << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1564
final_rank: 3

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



vint res;
int tot = 0;
bool solved = false;

void rec(vint num, int rem, vint vec) {
    //cout << num << ",,, " << sum << ";;;; " << vec << endl;
    
    if (solved) return;
    if (rem == 0) {
        res = vec;
        solved = true;
        return;
    }
    for (int d = 1; d <= 13; ++d) {
        if (num[d] == 0) continue;
        if ( (rem-d) % d != 0 ) continue;
        num[d]--;
        vint nvec = vec; nvec.PB(d);
        rec(num, rem-d, nvec);
        num[d]++;
    }
}

int main() {
    int n;
    while (cin >> n) {
        if (n == 0) break;
        vector<int> num(14, 0);
        tot = 0;
        for (int i = 0; i < n; ++i) {
            int c; cin >> c; num[c]++;
            tot += c;
        }
        res.clear();
        solved = false;
        vint kara;
        rec(num, tot, kara);
        reverse(ALL(res));
        
        if (res.size() == 0) puts("No");
        else {
            for (int i = 0; i < res.size(); ++i) {
                cout << res[i];
                if (i != res.size()-1) cout << " ";
            }
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 4160
memory_kb: 1244
final_rank: 15

code_snippet: #include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
int pow5[14];
set<int>S;
int ans[60];
int n;
int solve(int a,int b,int c){
	if(a==n)return 1;
	if(S.count(b))return 0;
	S.insert(b);
	for(int i=0;i<13;i++){
		if(b%pow5[i+1]/pow5[i]&&c%(i+1)==0){
			ans[n-1-a]=i+1;
			if(solve(a+1,b-pow5[i],c-(i+1)))return 1;
		}
	}
	return 0;
}
int main(){
	pow5[0]=1;
	for(int i=1;i<14;i++)pow5[i]=pow5[i-1]*5;
	int a;
	while(scanf("%d",&a),a){
		n=a;
		int st=0;
		int sum=0;
		for(int i=0;i<a;i++){
			int p;scanf("%d",&p);
			sum+=p;
			p--;
			st+=pow5[p];
		}
		S.clear();
		int res=solve(0,st,sum);
		if(res){
			for(int i=0;i<a;i++){
				if(i)printf(" ");
				printf("%d",ans[i]);
			}
			printf("\n");
		}else printf("No\n");
	}
}
accuracy: 1
time_ms: 20
memory_kb: 1292
final_rank: 1