Advertisement
Josif_tepe

Untitled

Apr 13th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int n, m;
  5. cin >> n >> m;
  6.  
  7. int niza[n];
  8.  
  9. int pref_sum[n];
  10. int sum = 0;
  11. for(int i = 0; i < n; i++) {
  12. cin >> niza[i];
  13.  
  14. sum += niza[i];
  15. pref_sum[i] = sum;
  16. }
  17. int res = 0;
  18. for(int i = 0; i < n; i++) {
  19. int L = i, R = n - 1;
  20. int idx = -1;
  21. while(L <= R) {
  22. int middle = (L + R) / 2;
  23.  
  24. if(i == 0) {
  25. sum = pref_sum[middle];
  26. }
  27. else {
  28. sum = pref_sum[middle] - pref_sum[i - 1];
  29. }
  30.  
  31. if(sum <= m) {
  32. if(middle > idx) {
  33. idx = middle;
  34. }
  35. L = middle + 1;
  36. }
  37. else {
  38. R = middle - 1;
  39. }
  40.  
  41. }
  42. if(idx - i + 1 > res) {
  43. res = idx - i + 1;
  44. }
  45. }
  46.  
  47. cout << res << endl;
  48.  
  49.  
  50. return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement