fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main(){
  5. int n,k;
  6. cin>>n>>k;
  7. vector<int>v(n);
  8. for(auto &it:v)cin>>it;
  9. int i=0,j=0,s=0,cnt=0;
  10. while(j<n){
  11. s+=v[j];
  12. if(s<=k){
  13. cnt+=j-i+1;
  14. j++;
  15. }
  16. else{
  17. s-=v[i];
  18. s-=v[j];
  19. i++;
  20. }
  21. }
  22. cout<<cnt<<"\n";
  23. }
Success #stdin #stdout 0s 5320KB
stdin
5 4
2 1 1 5 8
stdout
6