classSolution { public: intchalkReplacer(vector<int>& chalk, int k){ int n = chalk.size(); vector<long> s(n + 1, 0); for (int i = 1; i <= n; i++) { s[i] = s[i - 1] + chalk[i - 1]; } int cnt = 0; int m = (int)(k % s[n]); int l = 0, r = n - 1; while (l < r) { int mid = l + r >> 1; if (s[mid] >= m) { r = mid; } else { l = mid + 1; } } return s[l] <= m ? l : l - 1; } };