#include #include #define rep(i,n) for (int i = 0; i < (n); ++i) #define repp(i,n,m) for (int i = m; i < (n); ++i) using namespace std; using namespace atcoder; using namespace internal; using ll = long long; template bool chmax(SG &a, const SG &b){if(a,pair>; using LI = pair; int main(){ int n; cin >> n; int ans = 0; vector cnt(9,0); rep(i,9) cin >> cnt[i]; queue que; que.push(VP(cnt,LI(0,0))); vector ju(18,1LL); repp(i,18,1) ju[i] = ju[i-1] * 10LL; vector ni(49,1LL); repp(i,49,1) ni[i] = ni[i-1] * 2LL; while (!que.empty()){ VP p = que.front(); que.pop(); vector v = p.first; ll m = p.second.first; int d = p.second.second; if (d == n){ int score = 0; while (m % 2LL == 0LL){ score++; m /= 2LL; } chmax(ans,score); continue; } rep(i,9){ if (v[i] == 0) continue; ll newdegit = ll(i + 1) * ju[d]; if ((m + newdegit) % ni[d+1] == 0){ chmax(ans,d+1); auto q = v; q[i]--; que.push(VP(q,LI(m+newdegit,d+1))); } } } cout << ans << endl; }