Advertisement
Josif_tepe

Untitled

Jul 13th, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   int n, m;
  7.   cin >> n >> m;
  8.  
  9.   int niza[n];
  10.   int pref_sums[n];
  11.   int sum = 0;
  12.   for(int i = 0; i < n; i++) {
  13.     cin >> niza[i];
  14.    
  15.     sum += niza[i];
  16.     pref_sums[i] = sum;
  17.   }  
  18.   int res = 0;
  19.   for(int i = 0; i < n; i++) {
  20.     int L = i + 1;
  21.     int R = n - 1;
  22.  
  23.     while(L <= R) {
  24.       int middle = (L + R) / 2;
  25.  
  26.       if(i == 0) {
  27.           sum = pref_sums[middle];
  28.       }
  29.       else {
  30.         sum = pref_sums[middle] - pref_sums[i - 1];
  31.       }
  32.       if(sum <= m) {
  33.           res = max(res, middle - i + 1);
  34.           L = middle + 1;
  35.       }
  36.       else {
  37.         R = middle - 1;
  38.       }
  39.     }
  40.   }
  41.   cout << res << endl;
  42.  
  43.   return 0;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement