#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (ll)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int maxn = 2e6 + 100;
int n, num[maxn], q[maxn], ok[maxn];
ll m, sum = 0, f[maxn];
struct fake_set{
priority_queue <ll, vector <ll>, greater <ll>> good, bad;
int sz = 0;
void add(ll x) {
sz++;
good.push(x);
}
void er(ll x) {
sz--;
bad.push(x);
}
ll get() {
while (1) {
ll t = good.top();
ll v = sz(bad) ? bad.top() : -1e16;
if (t == v) {
bad.pop();
good.pop();
} else {
return t;
}
}
return 0;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
mem(f, 0x3f);
f[0] = 0;
for(int i = 1; i <= n; ++i) {
cin >> num[i];
}
fake_set pq;
int head = 0, tail = 0, pos = 0;
ll sum = 0;
for (int i = 1; i <= n; ++i) {
sum += num[i];
while (sum > m)
sum -= num[++pos];
while (head <= tail && q[head] <= pos) {
++head;
if (ok[head]) {
ok[head] = 0;
pq.er(f[q[head - 1]] + num[q[head]]);
}
}
while (head <= tail && num[q[tail]] <= num[i]) {
if (ok[tail]) {
ok[tail] = 0;
pq.er(f[q[tail - 1]] + num[q[tail]]);
}
--tail;
}
q[++tail] = i;
ok[tail] = 0;
if (tail > head) {
pq.add(f[q[tail - 1]] + num[q[tail]]);
ok[tail] = 1;
}
f[i] = f[pos] + num[q[head]] - 1;
if (pq.sz)
f[i] = min(f[i], pq.get() - 1);
}
cout << f[n] + n << '\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwoKI2RlZmluZSAgICAgICAgICAgZiAgICAgZmlyc3QKI2RlZmluZSAgICAgICAgICAgcyAgICAgc2Vjb25kCiNkZWZpbmUgICAgICAgICAgIHBiICAgIHB1c2hfYmFjawojZGVmaW5lICAgICAgICAgICBlcCAgICBlbXBsYWNlCiNkZWZpbmUgICAgICAgICAgIGViICAgIGVtcGxhY2VfYmFjawojZGVmaW5lICAgICAgICAgICBsYiAgICBsb3dlcl9ib3VuZAojZGVmaW5lICAgICAgICAgICB1YiAgICB1cHBlcl9ib3VuZAojZGVmaW5lICAgICAgIGFsbCh4KSAgICB4LmJlZ2luKCksIHguZW5kKCkKI2RlZmluZSAgICAgIHJhbGwoeCkgICAgeC5yYmVnaW4oKSwgeC5yZW5kKCkKI2RlZmluZSAgIHVuaXF1ZXYodikgICAgc29ydChhbGwodikpLCAodikucmVzaXplKHVuaXF1ZShhbGwodikpIC0gKHYpLmJlZ2luKCkpCiNkZWZpbmUgICAgIG1lbShmLHgpICAgIG1lbXNldChmICwgeCAsIHNpemVvZihmKSkKI2RlZmluZSAgICAgICAgc3ooeCkgICAgKGxsKSh4KS5zaXplKCkKI2RlZmluZSAgX19sY20oYSwgYikgICAgKDFsbCAqICgoYSkgLyBfX2djZCgoYSksIChiKSkpICogKGIpKQojZGVmaW5lICAgICAgICAgIG14eCAgICAqbWF4X2VsZW1lbnQKI2RlZmluZSAgICAgICAgICBtbm4gICAgKm1pbl9lbGVtZW50CiNkZWZpbmUgICAgY250Yml0KHgpICAgIF9fYnVpbHRpbl9wb3Bjb3VudGxsKHgpCiNkZWZpbmUgICAgICAgbGVuKHgpICAgIChpbnQpKHgubGVuZ3RoKCkpCgpjb25zdCBpbnQgbWF4biA9IDJlNiArIDEwMDsKaW50IG4sIG51bVttYXhuXSwgcVttYXhuXSwgb2tbbWF4bl07IApsbCBtLCBzdW0gPSAwLCBmW21heG5dOwoKc3RydWN0IGZha2Vfc2V0ewogICAgcHJpb3JpdHlfcXVldWUgPGxsLCB2ZWN0b3IgPGxsPiwgZ3JlYXRlciA8bGw+PiBnb29kLCBiYWQ7CiAgICBpbnQgc3ogPSAwOwogICAgdm9pZCBhZGQobGwgeCkgewogICAgICAgIHN6Kys7CiAgICAgICAgZ29vZC5wdXNoKHgpOwogICAgfQoKICAgIHZvaWQgZXIobGwgeCkgewogICAgICAgIHN6LS07CiAgICAgICAgYmFkLnB1c2goeCk7CiAgICB9CgogICAgbGwgZ2V0KCkgewogICAgICAgIHdoaWxlICgxKSB7CiAgICAgICAgICAgIGxsIHQgPSBnb29kLnRvcCgpOwogICAgICAgICAgICBsbCB2ID0gc3ooYmFkKSA/IGJhZC50b3AoKSA6IC0xZTE2OwoKICAgICAgICAgICAgaWYgKHQgPT0gdikgewogICAgICAgICAgICAgICAgYmFkLnBvcCgpOwogICAgICAgICAgICAgICAgZ29vZC5wb3AoKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHJldHVybiB0OwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gMDsKICAgIH0KfTsKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CiAgICBjb3V0LnRpZSgwKTsKCiAgICBjaW4gPj4gbiA+PiBtOwogICAgbWVtKGYsIDB4M2YpOwoKICAgIGZbMF0gPSAwOwoKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CiAgICAgICBjaW4gPj4gbnVtW2ldOwogICAgfQoKICAgIGZha2Vfc2V0IHBxOwogICAgaW50IGhlYWQgPSAwLCB0YWlsID0gMCwgcG9zID0gMDsKICAgIGxsIHN1bSA9IDA7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgKytpKSB7CiAgICAgICAgc3VtICs9IG51bVtpXTsKICAgICAgICB3aGlsZSAoc3VtID4gbSkKICAgICAgICAgICAgc3VtIC09IG51bVsrK3Bvc107CgogICAgICAgIHdoaWxlIChoZWFkIDw9IHRhaWwgJiYgcVtoZWFkXSA8PSBwb3MpIHsKICAgICAgICAgICAgKytoZWFkOwogICAgICAgICAgICBpZiAob2tbaGVhZF0pIHsKICAgICAgICAgICAgICAgIG9rW2hlYWRdID0gMDsKICAgICAgICAgICAgICAgIHBxLmVyKGZbcVtoZWFkIC0gMV1dICsgbnVtW3FbaGVhZF1dKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgKGhlYWQgPD0gdGFpbCAmJiBudW1bcVt0YWlsXV0gPD0gbnVtW2ldKSB7CiAgICAgICAgICAgIGlmIChva1t0YWlsXSkgewogICAgICAgICAgICAgICAgb2tbdGFpbF0gPSAwOwogICAgICAgICAgICAgICAgcHEuZXIoZltxW3RhaWwgLSAxXV0gKyBudW1bcVt0YWlsXV0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIC0tdGFpbDsKICAgICAgICB9CgogICAgICAgIHFbKyt0YWlsXSA9IGk7CiAgICAgICAgb2tbdGFpbF0gPSAwOwoKICAgICAgICBpZiAodGFpbCA+IGhlYWQpIHsKICAgICAgICAgICAgcHEuYWRkKGZbcVt0YWlsIC0gMV1dICsgbnVtW3FbdGFpbF1dKTsKICAgICAgICAgICAgb2tbdGFpbF0gPSAxOwogICAgICAgIH0KCiAgICAgICAgZltpXSA9IGZbcG9zXSArIG51bVtxW2hlYWRdXSAtIDE7CiAgICAgICAgaWYgKHBxLnN6KQogICAgICAgICAgICBmW2ldID0gbWluKGZbaV0sIHBxLmdldCgpIC0gMSk7CiAgICB9CgogICAgY291dCA8PCBmW25dICsgbiA8PCAnXG4nOwoKICAgIHJldHVybiAwOwp9Cg==