Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main() {
- int n, m;
- cin >> n >> m;
- int niza[n];
- int pref_sum[n];
- int sum = 0;
- for(int i = 0; i < n; i++) {
- cin >> niza[i];
- sum += niza[i];
- pref_sum[i] = sum;
- }
- int res = 0;
- for(int i = 0; i < n; i++) {
- int L = i, R = n - 1;
- int idx = -1;
- while(L <= R) {
- int middle = (L + R) / 2;
- if(i == 0) {
- sum = pref_sum[middle];
- }
- else {
- sum = pref_sum[middle] - pref_sum[i - 1];
- }
- if(sum <= m) {
- if(middle > idx) {
- idx = middle;
- }
- L = middle + 1;
- }
- else {
- R = middle - 1;
- }
- }
- if(idx - i + 1 > res) {
- res = idx - i + 1;
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement