tuki2501

nkmaxseq.cpp

Nov 7th, 2021
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 50005;
  5.  
  6. int a[N], s[N], b[N];
  7.  
  8. int main() {
  9.   cin.tie(0)->sync_with_stdio(0);
  10.   int n, p;
  11.   cin >> n >> p;
  12.   for (int i = 1; i <= n; i++) {
  13.     cin >> a[i];
  14.     s[i] = a[i] + s[i - 1];
  15.     if (s[i] < s[b[i - 1]]) b[i] = i;
  16.     else b[i] = b[i - 1];
  17.   }
  18.   int ans = -1;
  19.   {
  20.     int j = n;
  21.     for (int i = n; i >= 1; i--) {
  22.       if (j > i) j = i;
  23.       while (j > 0 && s[i] - s[b[j - 1]] >= p) j = b[j - 1];
  24.       if (s[i] - s[j] >= p) ans = max(ans, i - j);
  25.     }
  26.   }
  27.   cout << ans << '\n';
  28. }
Advertisement
Add Comment
Please, Sign In to add comment