fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n;
  7. cin>>n;
  8. vector<int>arr(n);
  9. for(int i=0;i<n;i++){
  10. cin>>arr[i];
  11. }
  12. int k;
  13. cin>>k;
  14. int maxi=-100;
  15. int sum=0;
  16. int j=0;
  17. for(int i=0;i<n;i++){
  18. sum+=arr[i];
  19. while(sum>k){
  20. sum=sum-arr[j];
  21. j++;
  22. }
  23. int l=i-j+1;
  24. maxi=max(maxi,l);
  25.  
  26. }
  27. cout<<maxi;
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5308KB
stdin
5
1 2 3 4 6
10
stdout
4