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. int k;
  9. cin>>k;
  10. int arr[n];
  11. for(int i=0;i<n;i++)
  12. {
  13. cin>>arr[i];
  14. }
  15.  
  16. int i=0,j=0,sum=0,count=0;
  17. while(i<n && j<n)
  18. {
  19. sum+=arr[j];
  20. while(sum>k)
  21. {
  22. sum-=arr[i];
  23. i++;
  24. }
  25. count+=j-i+1;
  26. j++;
  27. }
  28. cout<<count;
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5284KB
stdin
15 4 
1 2 3 5 8 8 8 8 2 1 1 1 1 1 1 
stdout
25