Problem:
ばらばら饅頭
会津若松市にある和菓子屋・友栗堂の店長さんは、とても腕のいい職人さんなのにちょっと気分屋なのが玉にキズです。店長さんの作る饅頭はとても美味しいのですが、その時の気分によって大きさがまちまちになってしまいます。
見かねた店長さんの奥さんは、大きさと重さがばらばらな饅頭を袋に詰めて売り出すことを思いつきました。一定の重さになるように饅頭を袋に詰めて売れば、一つ一つの大きさはばらばらでも一定の値段で売り出すことが出来ますし、どんな饅頭が入っているのか開けるまでわからないというサプライズも売りになるかもしれません。
「ばらばら饅頭」という名前で売りだしたところ、新しいアイディアが話題となり友栗堂はたちまち評判となりました。しかし、問題もあり、袋に入れる饅頭の組み合わせ方によって作れる「ばらばら饅頭」の数が変わり、無駄が発生してしまうことがありました。アルバイトで友栗堂に来ていたあなたは無駄なく袋に詰めるためにプログラムをつくることにしました。
饅頭の重さは1から9の9種類あります。袋に詰める時には、饅頭の重さの合計がぴったり10になるように詰め込みます。饅頭の組み合わせ方は1+1+2+4+2=10や1+1+1+1+1+5=10などで、同じ重さの饅頭をいくつ使っても構いません。
作られる饅頭の個数と饅頭の重さの情報を入力とし、作ることのできる「ばらばら饅頭」の最大の数を出力するプログラムを作成してください。
入力
複数のデータセットが与えられます。入力の終わりはゼロひとつで示されます。各データセットは以下の形式で与えられます。
n
m
1
m
2
...
m
n
1行目に饅頭の個数
n
(2 ≤
n
≤ 100) が与えられます。
2行目に各饅頭の重さ
m
i
(1 ≤
m
i
≤ 9) が与えられます。
データセットの数は 100 を超えません。
出力
データセットごとに、詰め込むことのできた「ばらばら饅頭」の最大の数を1行に出力します。
入力例
5
4 9 1 3 8
10
8 5 3 6 2 1 4 5 4 5
9
5 7 3 8 2 9 6 4 1
0
出力例
1
4
4

Submitted solutions:

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

using namespace std;

int accum(const vector<int>& a) {
    int s = 0;
    for (int i = 0; i < a.size(); ++i) {
        s += i * a[i];
    }
    return s;
}

map<vector<int>, int> m;

int rec(vector<int>& a, int w) {
    if (m.find(a) != m.end()) return m[a];
    if (accum(a) + w < 10) return 0;
    if (w > 10) {
        return 0;
    }
    int ans = 0;
    for (int i = 1; i < 10; ++i) {
        if (a[i] > 0) {
            --a[i];
            if (w + i == 10) ans = max(ans, rec(a, 0) + 1);
            else ans = max(ans, rec(a, w + i));
            ++a[i];
        }
    }
    m[a] = ans;
    return ans;
}

void run_test(int n) {
    m.clear();
    vector<int> a(10);
    for (int x, i = 0; i < n; ++i) {
        cin >> x;
        ++a[x];
    }
    int ans = 0;
    for (int i = 1; i < 5; ++i) {
        int cnt = min(a[i], a[10 - i]);
        a[i] -= cnt;
        a[10 - i] -= cnt;
        ans += cnt;
    }
    ans += a[5] / 2;
    a[5] %= 2;
    ans += rec(a, 0);
    cout << ans << '\n';
}

int main() {
    int n;
    cin >> n;
    while (n > 0) {
        run_test(n);
        cin >> n;
    }
    /*for (int i = 0; i < 100; ++i) {
        cout << 100 << '\n';
        for (int j = 0; j < 100; ++j) cout << 1 << ' ';
        cout << '\n';
    }
    cout << 0;*/
}
accuracy: 1
time_ms: 1780
memory_kb: 68184
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <map>
#define Release

using namespace std;

int max_ans;

int accum(const vector<int>& a) {
    int s = 0;
    for (int i = 0; i < a.size(); ++i) {
        s += i * a[i];
    }
    return s;
}

map<vector<int>, int> m;

int rec(vector<int>& a, int w) {
    if (m.find(a) != m.end()) return m[a];
    if (accum(a) + w < 10) return 0;
    if (w > 10) {
        return 0;
    }
    int ans = 0;
    for (int i = 1; i < 10; ++i) {
        if (a[i] > 0) {
            --a[i];
            if (w + i == 10) ans = max(ans, rec(a, 0) + 1);
            else ans = max(ans, rec(a, w + i));
            ++a[i];
        }
    }
    m[a] = ans;
    return ans;
}

void run_test(int n) {
    m.clear();
    vector<int> a(10);
    for (int x, i = 0; i < n; ++i) {
        cin >> x;
        ++a[x];
    }
    int ans = 0;
    for (int i = 1; i < 5; ++i) {
        int cnt = min(a[i], a[10 - i]);
        a[i] -= cnt;
        a[10 - i] -= cnt;
        ans += cnt;
    }
    ans += a[5] / 2;
    a[5] %= 2;
    max_ans = 0;
    ans += rec(a, 0);
    cout << ans << '\n';
}

int main() {
#ifdef Release
    int n;
    cin >> n;
    while (n > 0) {
        run_test(n);
        cin >> n;
    }
#endif // Release
    //run();
}
accuracy: 1
time_ms: 1800
memory_kb: 68348
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <map>
#define Release

using namespace std;

int max_ans;

int accum(const vector<int>& a) {
    int s = 0;
    for (int i = 0; i < a.size(); ++i) {
        s += i * a[i];
    }
    return s;
}

map<vector<int>, int> m;

int rec(vector<int>& a, int w) {
    if (m.find(a) != m.end()) return m[a];
    if (accum(a) + w < 10) return 0;
    if (w > 10) {
        return 0;
    }
    int ans = 0;
    for (int i = 1; i < 10; ++i) {
        if (a[i] > 0) {
            --a[i];
            if (w + i == 10) ans = max(ans, rec(a, 0) + 1);
            ans = max(ans, rec(a, w + i));
            ++a[i];
        }
    }
    m[a] = ans;
    return ans;
}

void run_test(int n) {
    vector<int> a(10);
    for (int x, i = 0; i < n; ++i) {
        cin >> x;
        ++a[x];
    }
    int ans = 0;
    for (int i = 1; i < 5; ++i) {
        int cnt = min(a[i], a[10 - i]);
        a[i] -= cnt;
        a[10 - i] -= cnt;
        ans += cnt;
    }
    ans += a[5] / 2;
    a[5] %= 2;
    max_ans = 0;
    ans += rec(a, 0);
    cout << ans << '\n';
}

int main() {
#ifdef Release
    int n;
    cin >> n;
    while (n > 0) {
        run_test(n);
        cin >> n;
    }
#endif // Release
    //run();
}
accuracy: 0.125
time_ms: 40
memory_kb: 9456
final_rank: 20

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

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> pii;

// Function to generate all possible subsets of weights 1..9 that sum to 10 with more than 2 buns
VVI generate_subsets(int i, int s, VI current, VVI &subsets) {
    if (i == 0) {
        if (s == 10 && accumulate(current.begin(), current.end(), 0) > 2) {
            subsets.emplace_back(current);
        }
        return subsets;
    }
    for(int k =0; k <= (10 - s)/i; ++k){
        current[i] = k;
        generate_subsets(i-1, s + k*i, current, subsets);
    }
    return subsets;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    VVI subsets;
    VI current(10, 0);
    generate_subsets(9, 0, current, subsets);
    int L = subsets.size();
    int n;
    while(cin >> n && n !=0){
        VI weights(n);
        for(auto &w : weights) cin >> w;
        VI C(10, 0);
        for(auto &w : weights){
            if(w >=1 && w <=9){
                C[w]++;
            }
        }
        int ans =0;
        for(int k=1; k<=4; ++k){
            int c = min(C[k], C[10 -k]);
            C[k] -=c;
            C[10 -k] -=c;
            ans +=c;
        }
        int c = C[5] /2;
        C[5] -=c*2;
        ans +=c;
        // Memoization
        unordered_map<string, int> memo[L];
        // DFS function
        function<int(int, VI)> dfs = [&](int idx, VI state) -> int{
            if(idx == L) return 0;
            string key = "";
            for(int j=1; j<=9; ++j) key += to_string(state[j]) + ",";
            if(memo[idx].find(key) != memo[idx].end()) return memo[idx][key];
            int res =0;
            // Try all possible k
            VI rc = subsets[idx];
            int mk = INT32_MAX;
            for(int j=1; j<=9; ++j){
                if(rc[j] >0){
                    mk = min(mk, state[j]/rc[j]);
                }
            }
            for(int k_val=0; k_val <= mk; ++k_val){
                VI new_state = state;
                bool valid = true;
                for(int j=1; j<=9; ++j){
                    new_state[j] -= rc[j]*k_val;
                    if(new_state[j] <0){
                        valid = false;
                        break;
                    }
                }
                if(valid){
                    res = max(res, dfs(idx+1, new_state) + k_val);
                }
            }
            return memo[idx][key] = res;
        };
        ans += dfs(0, C);
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 3160
memory_kb: 75992
final_rank: 14

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




int f(int n){
	int rr[102][911];
	memset(rr,-1,sizeof(rr));
	int ds[101];
	for(int i=0;i<n;i++){
		cin>>ds[i];
	}
	int ans=0;
	int sum=0;
	rr[0][0]=0;
	for(int i=0;i<n;i++){
		int d=ds[i];
		for(int j=sum;j>=0;j--){
			if(rr[i][j]==-1)continue;
			int x=rr[i][j];
			int add=0;
			for(int k=j;k<=sum;k++){
				if(rr[i][k]==-1)continue;
				int t=k+d;
				if(t%10==0&&k>j){
					add++;
					
				}
				rr[i+1][t]=max(rr[i+1][t],add);
				
			}
		}
		sum+=d;
		for(int j=0;j<=sum;j++){
			rr[i+1][j]=max(rr[i+1][j],rr[i][j]);
			ans=max(ans,rr[i+1][j]);
		}
		
		//for(int j=0;j<21;j++){
		//	cout<<" "<<rr[i+1][j];
		//}
		//cout<<endl;
	}
	return ans;
}

int main() {
	// your code goes here
	int n;
	while(true){
		cin>>n;
		if(n==0)break;
		cout<<f(n)<<endl;
	}
	return 0;
}
accuracy: 0.25
time_ms: 230
memory_kb: 3452
final_rank: 17

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

int dp[101][1001];
const int l=901;
int main() {
	// your code goes here
	while(true){
		int n;
		cin>>n;
		if(n==0)break;
		memset(dp,-1,sizeof(dp));
		dp[0][0]=0;
		int ans=0;
		for(int i=0;i<n;i++){
			int m;
			cin>>m;
			for(int j=l;j>=m;j--){
				int m2=j-m;
				if(dp[i][m2]>-1){
					if(j%10==0&&(dp[i][m2]+1==(j/10))){
						if(dp[i+1][j]<dp[i][m2]+1){
							dp[i+1][j]=dp[i][m2]+1;
						}
					}else{
						dp[i+1][j]=max(dp[i+1][j],dp[i][m2]);
					}
				}
			}
			int m3=0;

			
			for(int j=l;j>=0;j--){
				dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
			}
			
			for(int j=10;j<l;j+=10){
				
				if(dp[i+1][j]>dp[i][j]){
					for(int k=1;k<11;k++){
						dp[i+1][j+k]++;
					}
				}
			}
			for(int j=0;j<l;j++){
				ans=max(dp[i+1][j],ans);
			//	cout<<(dp[i+1][j]==-1?9:dp[i+1][j]);
			}
			//cout<<endl;
			
			//cout<<(dp[i+1][j]==-1?9:dp[i+1][j]);
		}
		cout<<ans<<endl;
	}
	return 0;
}
accuracy: 0.25
time_ms: 20
memory_kb: 3452
final_rank: 16

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
const double pi = 3.141592653589793238462643383279;
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).endf()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).endf(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).endf())
#define SORT(c) sort((c).begin(), (c).endf())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

#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 sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const double EPS = 1e-4, PI = acos(-1);
//ここから編集    
typedef string::const_iterator State;
map<vector<int>, int> dp;
int rec(vector<int> &v, int sum) {
    if(dp.count(v)) return dp[v];
    int res = 0;
    for(int i=1; i<=9; i++){
        if(v[i] > 0 && sum+i <= 10){
            v[i]--;
            res = max(res, rec(v, (sum+i)%10) + (sum+i)/10);
            v[i]++;
        }
    }
    return dp[v] = res;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int n;
    while(cin >> n, n){
        
        vector<int> cnt(10, 0);
        REP(i,n){
            int a; cin >> a;
            cnt[a]++;
        }

        int ans = 0;
        for(int i=1; i<=4; i++){
            int mi = min(cnt[i], cnt[10-i]);
            ans += mi;
            cnt[i] -= mi;
            cnt[10-i] -= mi;
        }
        ans += cnt[5]/2;
        cnt[5] = cnt[5]%2;
        dp.clear();
        ans += rec(cnt, 0);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 1980
memory_kb: 68244
final_rank: 10

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

map<vector<int>, int> dp;

int Dfs(vector<int> &ko, int s){
    if(dp.count(ko)) return dp[ko];

    int res = 0;
    for(int i=1;i<10;i++){
        if(ko[i] == 0) continue;
        if(s + i <= 10){
            ko[i]--;
            res = max(res, Dfs(ko, (s + i) % 10) + (s + i) / 10);
            ko[i]++;
        }
    }

    return dp[ko] = res;
}

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

    while(1){
        int n; cin>>n; if(n == 0) break;
        dp.clear();
        vector<int> ko(10,0);
        int ans = 0;
        rep(i,n){
            int a; cin>>a; ko[a]++;
        }

        for(int i=1;i<=4;i++){
            int c = min(ko[i], ko[10 - i]);
            ans += c; ko[i] -= c; ko[10 - i] -= c;
        }
        ans += ko[5] / 2; ko[5] %= 2;
        ans += Dfs(ko, 0);
        cout << ans << '\n';
    }

    return 0;
}
accuracy: 1
time_ms: 2210
memory_kb: 68244
final_rank: 11

code_snippet: #include<iostream>
#include<vector>
using namespace std;
int N;
int cnt[10];
main()
{
	while(cin>>N,N)
	{
		for(int i=1;i<=9;i++)cnt[i]=0;
		int ans=0;
		for(int i=0;i<N;i++)
		{
			int m;cin>>m;cnt[m]++;
		}
		vector<pair<int,int> >X;
		for(int i=1;i<5;i++)
		{
			int L=cnt[i],R=cnt[10-i];
			if(L<=R)
			{
				ans+=L;
				X.push_back(make_pair(R-L,10-i));
			}
			else
			{
				ans+=R;
				X.push_back(make_pair(L-R,i));
			}
		}
		ans+=cnt[5]/2;
		X.push_back(make_pair(cnt[5]%2,5));
		vector<vector<vector<vector<vector<vector<int> > > > > >dp(X[0].first+1,
			vector<vector<vector<vector<vector<int> > > > >(X[1].first+1,
				vector<vector<vector<vector<int> > > >(X[2].first+1,
					vector<vector<vector<int> > >(X[3].first+1,
						vector<vector<int> >(X[4].first+1,
							vector<int>(10,-1))))));
		dp[0][0][0][0][0][0]=0;
		int mx=0;
		for(int a=0;a<=X[0].first;a++)
			for(int b=0;b<=X[1].first;b++)
				for(int c=0;c<=X[2].first;c++)
					for(int d=0;d<=X[3].first;d++)
						for(int e=0;e<=X[4].first;e++)
							for(int k=0;k<10;k++)
								if(dp[a][b][c][d][e][k]>=0)
		{
			int now=dp[a][b][c][d][e][k];
			if(mx<now)mx=now;
			if(a<X[0].first&&k+X[0].second<=10)
			{
				int nk=k+X[0].second;
				int nxt=now;
				if(nk==10)nk=0,nxt++;
				dp[a+1][b][c][d][e][nk]=max(dp[a+1][b][c][d][e][nk],nxt);
			}
			if(b<X[1].first&&k+X[1].second<=10)
			{
				int nk=k+X[1].second;
				int nxt=now;
				if(nk==10)nk=0,nxt++;
				dp[a][b+1][c][d][e][nk]=max(dp[a][b+1][c][d][e][nk],nxt);
			}
			if(c<X[2].first&&k+X[2].second<=10)
			{
				int nk=k+X[2].second;
				int nxt=now;
				if(nk==10)nk=0,nxt++;
				dp[a][b][c+1][d][e][nk]=max(dp[a][b][c+1][d][e][nk],nxt);
			}
			if(d<X[3].first&&k+X[3].second<=10)
			{
				int nk=k+X[3].second;
				int nxt=now;
				if(nk==10)nk=0,nxt++;
				dp[a][b][c][d+1][e][nk]=max(dp[a][b][c][d+1][e][nk],nxt);
			}
			if(e<X[4].first&&k+X[4].second<=10)
			{
				int nk=k+X[4].second;
				int nxt=now;
				if(nk==10)nk=0,nxt++;
				dp[a][b][c][d][e+1][nk]=max(dp[a][b][c][d][e+1][nk],nxt);
			}
		}
		cout<<ans+mx<<endl;
	}
}
accuracy: 1
time_ms: 190
memory_kb: 55112
final_rank: 5

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;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }



int main() {
    int n;
    while(cin >> n, n){
        array<int, 11> cnt{};
        for (int i = 0; i < n; ++i) {
            int x; cin >> x;
            cnt[x]++;
        }
        int ans = 0;
        while(cnt[1] && cnt[9]) cnt[1]--, cnt[9]--, ans++;
        while(cnt[2] && cnt[8]) cnt[2]--, cnt[8]--, ans++;
        while(cnt[3] && cnt[7]) cnt[3]--, cnt[7]--, ans++;
        while(cnt[4] && cnt[6]) cnt[4]--, cnt[6]--, ans++;
        while(cnt[5] >= 2) cnt[5] -= 2, ans++;
        vector<pair<int, int>> v;
        for (int i = 1; i < 9; ++i) if(cnt[i]) v.emplace_back(cnt[i], i);
        while(v.size() < 5) v.emplace_back(0, 0);
        using A = array<char, 5>;
        A x;
        for (int i = 0; i < v.size(); ++i) x[i] = v[i].first;
        vector<A> ok;
        for (char i = 0; i <= v[0].first; ++i) {
            for (char j = 0; j <= v[1].first; ++j) {
                for (char k = 0; k <= v[2].first; ++k) {
                    for (char l = 0; l <= v[3].first; ++l) {
                        for (char m = 0; m <= v[4].first; ++m) {
                            if(i*v[0].second+j*v[1].second+k*v[2].second+l*v[3].second+m*v[4].second == 10) ok.emplace_back(A{i, j, k, l, m});
                        }
                    }
                }
            }
        }
        map<A, int> dp;
        auto dfs = [&](A x, int dep, auto &&f) -> int {
            if(dp.count(x)) return dp[x];
            int ret = dep;
            for (int i = 0; i < ok.size(); ++i) {
                A y;
                for (int j = 0; j < 5; ++j) y[j] = x[j]-ok[i][j];
                if(*min_element(y.begin(),y.end()) >= 0) ret = max(ret, f(y, dep+1, f));
            }
            return dp[x] = ret;
        };
        cout << dfs(x, ans, dfs) << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 590
memory_kb: 6268
final_rank: 2

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

map<vector<int>, int> dp;

int rec(vector<int> &v, int sum){
    if(dp.count(v)) return dp[v];
    int res = 0;
    for(int i=9; i>=1; i--){
        if(v[i]>0 && sum+i<=10){
            v[i]--;
            res = max(res, rec(v, (sum+i)%10)+(sum+i)/10);
            v[i]++;
        }
    }
    return dp[v] = res;
}

int main() {
    int n;
    while(cin >> n, n){
        vector<int> v(10,0);
        int ans = 0;
        for(int i=0;i<n;i++){
            int a;
            cin >> a;
            v[a]++;
        }
        for(int i=1;i<=4;i++){
            int tmp = min(v[i], v[10-i]);
            ans += tmp;
            v[i] -= tmp;
            v[10-i] -= tmp;
        }
        ans += v[5]/2;
        v[5] %= 2;
        dp.clear();
        ans += rec(v, 0);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2630
memory_kb: 68112
final_rank: 13

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

map<vector<int>, int> dp;

int rec(vector<int> &v, int sum){
    if(dp.count(v)) return dp[v];
    int res = 0;
    for(int i=9; i>=1; i--){
        if(v[i]>0 && sum+i<=10){
            v[i]--;
            res = max(res, rec(v, (sum+i)%10)+(sum+i)/10);
            v[i]++;
        }
    }
    return dp[v] = res;
}

int main() {
    int n;
    while(cin >> n, n){
        vector<int> v(10,0);
        int ans = 0;
        for(int i=0;i<n;i++){
            int a;
            cin >> a;
            v[a]++;
        }
        for(int i=1;i<=4;i++){
            int tmp = min(v[i], v[10-i]);
            ans += tmp;
            v[i] -= tmp;
            v[10-i] -= tmp;
        }
        ans += v[5]/2;
        v[5] %= 2;
        ans += rec(v, 0);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 0.125
time_ms: 40
memory_kb: 9292
final_rank: 19

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

map<vector<int>, int> dp;

int rec(vector<int> &v, int sum){
    if(dp.count(v)) return dp[v];
    int res = 0;
    for(int i=1; i<=9; i++){
        if(v[i]>0 && sum+i<=10){
            v[i]--;
            res = max(res, rec(v, (sum+i)%10)+(sum+i)/10);
            v[i]++;
        }
    }
    return dp[v] = res;
}

int main() {
    int n;
    while(cin >> n, n){
        vector<int> v(10,0);
        int ans = 0;
        for(int i=0;i<n;i++){
            int a;
            cin >> a;
            v[a]++;
        }
        for(int i=1;i<=4;i++){
            int tmp = min(v[i], v[10-i]);
            ans += tmp;
            v[i] -= tmp;
            v[10-i] -= tmp;
        }
        ans += v[5]/2;
        v[5] %= 2;
        ans += rec(v, 0);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 0.125
time_ms: 40
memory_kb: 9248
final_rank: 18

code_snippet: #include<algorithm>
#include<stdio.h>
#include<map>
using namespace std;
struct wolf{
	int t[9];
	wolf(){}
};
int v[9];
int D[36][9]={
{2,0,0,0,0,0,0,1,0},
{1,1,0,0,0,0,1,0,0},
{3,0,0,0,0,0,1,0,0},
{1,0,1,0,0,1,0,0,0},
{0,2,0,0,0,1,0,0,0},
{2,1,0,0,0,1,0,0,0},
{4,0,0,0,0,1,0,0,0},
{1,0,0,1,1,0,0,0,0},
{0,1,1,0,1,0,0,0,0},
{2,0,1,0,1,0,0,0,0},
{1,2,0,0,1,0,0,0,0},
{3,1,0,0,1,0,0,0,0},
{5,0,0,0,1,0,0,0,0},
{0,1,0,2,0,0,0,0,0},
{0,0,2,1,0,0,0,0,0},
{2,0,0,2,0,0,0,0,0},
{1,1,1,1,0,0,0,0,0},
{0,3,0,1,0,0,0,0,0},
{3,0,1,1,0,0,0,0,0},
{2,2,0,1,0,0,0,0,0},
{4,1,0,1,0,0,0,0,0},
{6,0,0,1,0,0,0,0,0},
{1,0,3,0,0,0,0,0,0},
{0,2,2,0,0,0,0,0,0},
{2,1,2,0,0,0,0,0,0},
{4,0,2,0,0,0,0,0,0},
{1,3,1,0,0,0,0,0,0},
{3,2,1,0,0,0,0,0,0},
{5,1,1,0,0,0,0,0,0},
{7,0,1,0,0,0,0,0,0},
{0,5,0,0,0,0,0,0,0},
{2,4,0,0,0,0,0,0,0},
{4,3,0,0,0,0,0,0,0},
{6,2,0,0,0,0,0,0,0},
{8,1,0,0,0,0,0,0,0},
{10,0,0,0,0,0,0,0,0}
};
long long conv(wolf a){
	long long ret=0;
	for(int i=0;i<9;i++){
		ret*=128;
		ret+=a.t[i];
	}
	return ret;
}
map<long long,int> dp;
int solve(wolf a,int b){
	if((int)(dp.count(conv(a))))return dp[conv(a)];
	int ret=0;
	for(int i=b;i<36;i++){
		bool ok=true;
		for(int j=0;j<9;j++){
			if(a.t[j]<D[i][j]){
				ok=false;
				break;
			}
		}
		if(ok){
			for(int j=0;j<9;j++)a.t[j]-=D[i][j];
			ret=max(ret,solve(a,i)+1);
			for(int j=0;j<9;j++)a.t[j]+=D[i][j];
		}
	}
	return dp[conv(a)]=ret;
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		dp.clear();
		for(int i=0;i<10;i++)v[i]=0;
		for(int i=0;i<a;i++){
			int b;
			scanf("%d",&b);
			b--;
			v[b]++;
		}
		int ret=0;
		for(int i=5;i<9;i++){
			int V=min(v[i],v[8-i]);
			ret+=V;
			v[i]-=V;
			v[8-i]-=V;
		}
		ret+=v[4]/2;
		v[4]%=2;
		wolf S;
		for(int i=0;i<9;i++)S.t[i]=v[i];
		printf("%d\n",ret+solve(S,0));
	}
}
accuracy: 1
time_ms: 110
memory_kb: 5928
final_rank: 1

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map< vector<int>, int > dp;

int solve(vector<int>& vec, int sum = 0, int id = 1) {
    int res = 0;
    if(sum == 10) {
        res++;
        sum = 0; id = 1;
    }
    if(dp.count(vec)) return dp[vec];

    int add = 0;
    for(int i=id; i<=9; i++) {
        if(vec[i] > 0 && sum + i <= 10) {
            vec[i]--; sum += i;
            chmax(add, solve(vec, sum, i));
            vec[i]++; sum -= i;
        }
    }
    res += add;

    if(sum == 0) dp[vec] = res;
    return res;
}

signed main() {
    while(cin >> N, N) {
        dp.clear();
        vector<int> countManju(11);

        for(int i=0; i<N; i++) {
            int val; cin >> val;
            countManju[val]++;
        }
        
        int ans = 0;
        for(int i=1; i<5; i++) {
            int add = min(countManju[i], countManju[10 - i]);
            ans += add;
            countManju[i     ] -= add;
            countManju[10 - i] -= add;
        }
        ans += countManju[5] / 2;
        countManju[5] %= 2;

        ans += solve(countManju);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2340
memory_kb: 12064
final_rank: 6

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map< vector<int>, int > dp;

int solve(vector<int>& vec, int sum = 0, int id = 1) {
    int res = 0;
    if(sum == 10) {
        res++;
        sum = 0; id = 1;
    }
    if(dp.count(vec)) return dp[vec];
    if(sum > 10) return 0;

    int add = 0;
    for(int i=id; i<=9; i++) {
        if(vec[i] > 0 && sum + i <= 10) {
            vec[i]--; sum += i;
            chmax(add, solve(vec, sum, i));
            vec[i]++; sum -= i;
        }
    }
    res += add;

    if(sum == 0) dp[vec] = res;
    return res;
}

signed main() {
    while(cin >> N, N) {
        dp.clear();
        vector<int> countManju(11);

        for(int i=0; i<N; i++) {
            int val; cin >> val;
            countManju[val]++;
        }
        
        int ans = 0;
        for(int i=1; i<5; i++) {
            int add = min(countManju[i], countManju[10 - i]);
            ans += add;
            countManju[i     ] -= add;
            countManju[10 - i] -= add;
        }
        ans += countManju[5] / 2;
        countManju[5] %= 2;

        ans += solve(countManju);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 2260
memory_kb: 12044
final_rank: 4

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map< vector<int>, int > dp;

int solve(vector<int>& vec, int sum = 0, int id = 1) {
    int res = 0;
    if(sum == 10) {
        res++;
        sum = 0; id = 1;
    }
    if(dp.count(vec)) return dp[vec];
    if(sum > 10) return 0;

    int add = 0;
    for(int i=id; i<=9; i++) {
        if(vec[i] > 0) {
            vec[i]--; sum += i;
            chmax(add, solve(vec, sum, i));
            vec[i]++; sum -= i;
        }
    }
    res += add;

    if(sum == 0) dp[vec] = res;
    return res;
}

signed main() {
    while(cin >> N, N) {
        dp.clear();
        vector<int> countManju(11);

        for(int i=0; i<N; i++) {
            int val; cin >> val;
            countManju[val]++;
        }
        
        int ans = 0;
        for(int i=1; i<5; i++) {
            int add = min(countManju[i], countManju[10 - i]);
            ans += add;
            countManju[i     ] -= add;
            countManju[10 - i] -= add;
        }
        ans += countManju[5] / 2;
        countManju[5] %= 2;

        ans += solve(countManju);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 3520
memory_kb: 12084
final_rank: 7

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int N;
map< vector<int>, int > dp;

int solve(vector<int>& vec, int sum = 0, int id = 1) {
    int res = 0;
    if(sum == 10) {
        res++;
        sum = 0; id = 1;
    }
    if(sum == 0 and dp.count(vec)) return dp[vec];
    if(id > 9 or sum + id > 10) return 0;

    int add = 0;
    if(vec[id] > 0) {
        vec[id]--;
        chmax(add, solve(vec, sum + id, id));
        vec[id]++;
    }
    chmax(add, solve(vec, sum, id + 1));
    res += add;

    if(sum == 0) dp[vec] = res;
    return res;
}

signed main() {
    while(cin >> N, N) {
        dp.clear();
        vector<int> countManju(11);

        for(int i=0; i<N; i++) {
            int val; cin >> val;
            countManju[val]++;
        }
        
        int ans = 0;
        for(int i=1; i<5; i++) {
            int add = min(countManju[i], countManju[10 - i]);
            ans += add;
            countManju[i     ] -= add;
            countManju[10 - i] -= add;
        }
        ans += countManju[5] / 2;
        countManju[5] %= 2;

        ans += solve(countManju);
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 1190
memory_kb: 12116
final_rank: 3

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;

bool check(const vector<vector<pair<int, int>>>&humans, int x, int y) {
	for (int i = 0; i < 4; ++i) {
		auto it=find(humans[i].begin(),humans[i].end(),make_pair(x,y));
		if(it!=humans[i].end())return false;
	}
	return true;
}
int dx[] = { -1,0,1,0,0 };
int dy[] = { 0,1,0,-1,0 };

template<typename T> struct Compress {
	map<T, int>mp;
	map<int, T>revmp;

	Compress(vector<T>vs) {
		setmp(vs);
	}

	Compress() :mp(), revmp() {

	}
	void setmp(vector<T>vs) {
		sort(vs.begin(), vs.end());
		vs.erase(unique(vs.begin(), vs.end()), vs.end());
		for (int i = 0; i < static_cast<int>(vs.size()); ++i) {
			mp[vs[i]] = i;
			revmp[i] = vs[i];
		}
	}

};
struct sale {
	int id;
	int d;
	int s;
	int e;
};

struct aa {
	int flag;
	int x;
	int y;
	int cost;
};

bool operator <(const vector<int>&l, const vector<int>&r) {
	for (int i = 0; i < l.size(); ++i) {
		if(l[i]<r[i])return true;
		else if(l[i]>r[i])return false;
	}
	return false;
}
int solve(vector<int>haves, vector<int>nums) {
	map<pair<vector<int>,int>,int>mp;
	mp[make_pair(nums,0)]=1;

	int ans=1;
	queue<pair<vector<int>,int>>que;
	que.push(make_pair(nums,0));
	while (!que.empty()) {
		auto atop(que.front());
		que.pop();

		int x=atop.second;
		vector<int>v(atop.first);

		int cost=mp[make_pair(atop.first,atop.second)];
		ans=max(ans,cost);
		for (int i = 0; i < haves.size(); ++i) {
			if (v[i]){
				if (x+haves[i] >= 11) {

				}
				else if (x + haves[i] == 10) {
					v[i]--;
					auto np=make_pair(v,0);
					if (mp[np] < cost + 1) {
						mp[np]=cost+1;
						que.push(np);
					}
					v[i]++;
				}
				else {
					v[i]--;
					auto np=make_pair(v,x+haves[i]);
					if (mp[np] < cost) {
						mp[np]=cost;
						que.push(np);
					}
					v[i]++;
				}
			}
		}
	}
	return ans-1;
}

int main()
{
	while (true) {
		int N;cin>>N;
		if(N==0)break;
		vector<int>vs(10);
		for (int i = 0; i < N; ++i) {
			int a;cin>>a;
			vs[a]++;

		}
		int ans=0;
		for (int i = 6; i <= 9; ++i) {
			int x=min(vs[i],vs[10-i]);
			vs[i]-=x;
			vs[10-i]-=x;
			ans+=x;
		}
		{
			int x=vs[5]/2;
			vs[5]-=2*x;
			ans+=x;
		}
		vector<int>haves;
		vector<int>nums;
		for (int i = 1; i <= 9; ++i) {
			if (vs[i]) {
				haves.push_back(i);
				nums.push_back(vs[i]);
			}
		}
		ans+=solve(haves,nums);
		cout<<ans<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 2980
memory_kb: 60276
final_rank: 12

code_snippet: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;

int main(){
    int n;
    while(cin >> n,n){
        int a[205];
        rep(i,n) cin >> a[i];
        sort(a,a + n,greater<int>());

        int ans = 0, f = true;
        map<pair<int, int>, int> pre;
        rep(i,n){
            rep(j,n){
                pre[make_pair(i,j)] = -1;
            }
        }

        int num = 2;
        while(f){
            f = false;
            bool dp[11][11] = {{0}};
            dp[0][0] = true;
            rep(i,n){
                if(a[i] == 0) continue;
                for(int k = num; k >= 0; k--){
                    if(a[i] == 0) continue;
                    for(int j = 10; j >= 0; j--){
                        if(j - a[i] < 0) continue;
                        if(not dp[j][k + 1] && dp[j - a[i]][k]){
                            //cout << i << ' ' << j << endl;
                            dp[j][k + 1] = true;
                            pre[make_pair(j,k + 1)] = i;
                            //cout << j << ' ' << k + 1 << ' ' << i << endl;
                        }
                    }
                }
            }
            //cin >> n;
            if(dp[10][num]){
                f = true;
                ans++;
                int sum = 10, k = num;
                while(sum != 0){
                    int prev = pre[make_pair(sum,k)];
                    //cout << prev.first << ' ' << prev.second << endl;
                    sum -= a[prev];
                    a[prev] = 0;
                    k--;
                    //show(sum)
                }
            }
            if(not f && num < 10){
                num++;
                f = true;
            }
        }
        cout << ans << endl;
    }
}
accuracy: 0.375
time_ms: 40
memory_kb: 3752
final_rank: 15