#include #include #include using namespace std; using i64 = long long; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); i64 l; cin >> l; vector v(l); for(int i=0; i> v[i]; i64 n; cin >> n; i64 ncycles = n / l; vector gv(l, v[0]); for(int i=1; i> height_length; vector imos1(l+1); vector imos0(l); i64 mask = ((i64)1 << 30) - 1; auto contribute = [&](i64 pos, i64 t, i64 weight) { if(t + pos <= ncycles){ imos0[pos] += weight * (ncycles - t); imos0[pos] &= mask; imos1[pos] -= weight; imos1[pos] &= mask; } else if(t <= ncycles){ i64 decrease_len = ncycles - t; imos0[pos] += weight * (ncycles - t); imos0[pos] &= mask; imos1[pos] -= weight; imos1[pos] &= mask; imos1[pos - decrease_len] += weight; imos1[pos - decrease_len] &= mask; } }; for(int i=l-1; i>=0; i--){ if(gv[i] == 0) continue; i64 rem = v[i]; i64 turn = 1; while(!height_length.empty() && rem >= gv[i]){ auto [h, len] = height_length.back(); height_length.pop_back(); contribute(i, turn, -h); contribute(i, turn + len, h); if(len == 0) continue; if(h == gv[i]){ turn += len; continue; } i64 hturn = (rem - gv[i]) / (gv[i] - h) + 1; if(hturn >= len){ turn += len; rem -= len * (gv[i] - h); } else{ rem -= hturn * (gv[i] - h); contribute(i, turn + hturn, h); contribute(i, turn + len, -h); turn += hturn; height_length.push_back({ h, len - hturn }); break; } } turn += rem / gv[i]; rem %= gv[i]; if(turn > ncycles + 100) turn = ncycles + 100; contribute(i, turn - 1, rem); contribute(i, turn, -rem); height_length.push_back({ rem, 1 }); turn -= 1; contribute(i, 0, gv[i]); contribute(i, turn, -gv[i]); height_length.push_back({ gv[i], turn }); } for(int i=l-1; i>=0; i--) imos1[i] = (imos1[i] + imos1[i+1]) & mask; for(int i=0; i ans(l); i64 rem = v[0]; for(int i=l-1; i>=0; i--){ i64 x = min(imos0[i], rem); rem -= x; ans[i] = x; } ans[0] += minv; for(i64 i=0; i<(n%l); i++){ i64 f = min(ans[i], v[i+1] + minv - ans[i+1]); ans[i] -= f; ans[i+1] += f; } for(int i=0; i