fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26. int consistency(int n, int k){
  27.  
  28. int sum = 0;
  29. int s = 0, e = 0;
  30. int ans = 0;
  31.  
  32. while(e<n){
  33. sum += a[e];
  34. if(sum < k){
  35. ans = max(ans, e-s+1);
  36. e++;
  37. }
  38. else{
  39. while(s<=e && sum > k){
  40. sum -= a[s];
  41. s++;
  42. }
  43. if(sum <= k){
  44. ans = max(ans, e-s+1);
  45. }
  46. e++;
  47. }
  48. }
  49.  
  50. return ans;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. int practice(int n, int m){
  63.  
  64. return 0;
  65. }
  66.  
  67.  
  68.  
  69. void solve() {
  70.  
  71. int n, m;
  72. cin>>n >> m;
  73.  
  74. a.resize(n);
  75. for(int i=0; i<n; i++) cin >> a[i];
  76.  
  77. cout << consistency(n, m) << endl;
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. int32_t main() {
  87. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  88.  
  89. int t = 1;
  90. // cin >> t;
  91. while (t--) {
  92. solve();
  93. }
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0.01s 5312KB
stdin
4 5
3 1 2 1
stdout
3