#include #include #include #include #include #include #include #include #include #include #include #include #define vll vector #define vvvl vector #define vvl vector> #define VV(a, b, c, d) vector>(a, vector(b, c)) #define VVV(a, b, c, d) vector(a, vvl(b, vll (c, d))); #define re(c, b) for(ll c=0;c> n >> k; ll l = 0, r = 10000000000;//box string s;std::cin >> s; ll S = 0; for(auto c:s) S += (c-'0'); map mp; while(r-l>1){ ll mid = (l+r)/2; ll eat = mid * n, num = mid * S; while(num){ ll pre = num; ll x = num/n; num %= n; if(x){ num += x*S; eat += x*n; }else{ ll idx = 0; while(num){ num--; eat++; if(s[idx]=='1') num++; if(s[idx]=='2') num+=2; idx = (idx + 1)%n; if(eat>k) break; } break; } if(pre>=num) { eat = k+1; break; } if(eat>k) break; } mp[mid] = eat; if(eat>k) r = mid; else l = mid; } for(int i=0;i<=n;i++){ ll eat = mp[l]; ll num = i; ll idx = eat%n; vll tbl(n, -1); tbl[idx] = num; while(num){ num--; eat++; if(s[idx]=='1') num++; if(s[idx]=='2') num+=2; idx = (idx + 1)%n; if(tbl[idx]!=-1&&tbl[idx]<=num){ std::cout << l*n+i << '\n'; return 0; } tbl[idx] = num; } if(eat>=k){ std::cout << l*n+i << '\n'; return 0; } } }