fork download
  1. #include <algorithm>
  2. #include <cstdio>
  3.  
  4. constexpr int MAXN = 3000000;
  5. static long long pref[MAXN + 1];
  6. static int idx[MAXN + 1];
  7.  
  8. class FastInput {
  9. char buf[1 << 16];
  10. int pos = 0, len = 0;
  11.  
  12. inline int get() {
  13. if (pos == len) {
  14. len = std::fread(buf, 1, sizeof(buf), stdin);
  15. pos = 0;
  16. if (!len) return EOF;
  17. }
  18. return buf[pos++];
  19. }
  20.  
  21. public:
  22. template<class T>
  23. inline T read() {
  24. int c = get();
  25. while (c != EOF && c <= ' ') c = get();
  26. bool neg = (c == '-');
  27. if (neg) c = get();
  28.  
  29. T x = 0;
  30. while (c >= '0' && c <= '9') {
  31. x = x * 10 + (c - '0');
  32. c = get();
  33. }
  34. return neg ? -x : x;
  35. }
  36. };
  37.  
  38. int main() {
  39. FastInput in;
  40. int n = in.read<int>();
  41. long long v = in.read<long long>();
  42.  
  43. int head = 0, tail = 1, ans = n + 1;
  44. long long sum = 0;
  45.  
  46. for (int r = 1; r <= n; ++r) {
  47. int x = in.read<int>();
  48. if (x >= v) {
  49. std::puts("1");
  50. return 0;
  51. }
  52.  
  53. sum += x;
  54. long long limit = sum - v;
  55.  
  56. if (pref[head] <= limit) {
  57. do {
  58. ++head;
  59. } while (head < tail && pref[head] <= limit);
  60. ans = std::min(ans, r - idx[head - 1]);
  61. }
  62.  
  63. while (head < tail && pref[tail - 1] >= sum) --tail;
  64. pref[tail] = sum;
  65. idx[tail++] = r;
  66. }
  67.  
  68. std::printf("%d\n", ans <= n ? ans : -1);
  69. }
Success #stdin #stdout 0s 544KB
stdin
5 6
5 1 3 2 4
stdout
2