fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, m;
  6. cin >> n >> m;
  7.  
  8. vector<int> tab(n+1);
  9. tab[0]=0;
  10.  
  11. for (int i=1; i<=n; i++) {
  12. cin >> tab[i];
  13. }
  14.  
  15. int c=1, d=1, s=1, k=0;
  16.  
  17. while (d <= n) {
  18. if (tab[d]-tab[d-1]<=0) {
  19. d++;
  20. s = max(s, d-c+1);
  21. } else {
  22. if (tab[d]-tab[d-1] <= m-k) {
  23. k+=tab[d]-tab[d-1];
  24. d++;
  25. s = max(s, d-c+1);
  26. } else {
  27. if (c<d) {
  28. if (tab[c]-tab[c-1]>0) {
  29. k-=tab[c]-tab[c-1];
  30. }
  31. c++;
  32. } else{
  33. d++;
  34. c++;
  35. }
  36. }
  37. }
  38. }
  39.  
  40. cout << s;
  41. return 0;
  42. }
Success #stdin #stdout 0s 5320KB
stdin
5 1
4 2 1 3 4
stdout
3