fork download
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int main(void){
  5. int n;
  6. cin>> n;
  7. int k;
  8. cin>>k;
  9. vector<int> arr(n);
  10. //take the input from the user
  11. for(int i=0;i<n;i++){
  12. cin>>arr[i];
  13. }
  14. int sum=0;
  15. int count=0;
  16. for(int i=0,j=0;j<n;j++){
  17. sum+=arr[j];
  18. while(sum>k){
  19. sum=sum-arr[i];
  20. i++;
  21. }
  22. count+=(j-i+1);
  23. }
  24. cout<<count;
  25. return 0;
  26. }
  27.  
  28.  
Success #stdin #stdout 0.01s 5308KB
stdin
15 4 
1 2 3 5 8 8 8 8 2 1 1 1 1 1 1 
stdout
25