#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

int n,st[100100],head=1,tail,pos[100100],active[100100];
long long s[100100],f[100100],m;
priority_queue < pair<long long,int> > q;

int main()
{
//    freopen("difmax.inp" , "r" , stdin);
//    freopen("difmax.ans" , "w" , stdout);
    int x;
    cin >> n >> m;
    for (int i=1,j=0;i<=n;i++)
    {
        scanf("%d",&x);
        if (x>m)
        {
            cout << -1 << endl;
            return 0;
        }

        while (tail>=head && x>=st[tail]) active[pos[tail--]]=0;
        st[++tail]=x; pos[tail]=i;

        s[i]=s[i-1]+x;
        while (s[i]-s[j]>m) j++;
        while (head<=tail && pos[head]<=j) active[pos[head++]]=0;
        active[pos[head]]=0;

        f[i]=f[j]+st[head];
        while (!q.empty())
        {
            pair <long long,int> u=q.top();
            if (!active[u.second]) q.pop();
            else
            {
                f[i]=min(f[i],-u.first);
                break;
            }
        }
        if (tail>head)
        {
            f[i]=min(f[i],f[pos[tail-1]]+st[tail]);
            q.push(make_pair(-f[pos[tail-1]]-st[tail],i));
        }
        else q.push(make_pair(0LL,i));
        active[i]=1;
    }
    cout << f[n] << endl;
}
