fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ios::sync_with_stdio(false);
  10. cin.tie(NULL);
  11. cout.tie(NULL);
  12.  
  13. int N, S;
  14. cin >> N >> S;
  15.  
  16. int* arr = new int[N];
  17.  
  18. for (int i = 0; i < N; i++)
  19. {
  20. cin >> arr[i];
  21. }
  22.  
  23. int start = 0;
  24. int end = 0;
  25. int sum = 0;
  26. int min = 100000;
  27.  
  28. while (end < N && start < N)
  29. {
  30. if (sum < S)
  31. {
  32. sum += arr[end++];
  33. }
  34. if (sum >= S)
  35. {
  36. sum -= arr[start++];
  37. if (min > (end - start + 1))
  38. {
  39. min = end - start + 1;
  40. }
  41. }
  42. }
  43.  
  44. if (min == 100000)
  45. {
  46. cout << 0;
  47. }
  48. else
  49. {
  50. cout << min;
  51. }
  52.  
  53. }
Success #stdin #stdout 0.01s 5296KB
stdin
10 10
1 1 1 1 1 1 1 1 1 10
stdout
10