#include #include using namespace std; using int64 = long long; int main() { int N, K; cin >> N >> K; string S; cin >> S; const int MAXN = 60; static pair stamp[MAXN]; // (その時点での使用金額, その時点でのアイスの数) for (int i = 0; i < N; i++) stamp[i] = {-1, -1}; stamp[0] = {0, K}; int64 ans = 0; int ptr = 0; // ループを見つけるかINFに入ったら終了 while (K > 0) { ans++; K--; int start = ptr, ice = S[ptr] - '0'; ptr = (ptr + 1) % N; while (ice > 0 and ptr != start) { K--; ice--; ice += S[ptr] - '0'; ptr = (ptr + 1) % N; } /* cerr << "ptr is " << ptr << endl; cerr << "K is " << K << endl; */ if (ptr == start and ice > 0) K = 0; if (K <= 0) break; if (stamp[ptr].first >= 0) { // ans-stamp[ptr].first円でstamp[ptr].second-K個のアイスを購入できる ans += (K / (stamp[ptr].second - K)) * (ans - stamp[ptr].first); K %= (stamp[ptr].second - K); } stamp[ptr] = {ans, K}; } cout << ans << endl; return 0; }