第一道。。MARK。。其实还没完全看懂呢,自己照着网上的写了下。。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
#define maxn 101000
#define inf 9999999
typedef long long ll;
ll a[maxn];
ll q[maxn];
ll sum[maxn];
ll dp[maxn];
int main()
{
freopen("in.txt","r",stdin);
int n;
ll m;
while(scanf("%lld%lld",&n,&m)==2&&(n||m))
{
int i,j;
sum[0]=0;
int flag=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>m)
flag=1;
sum[i]=sum[i-1]+a[i];
}
int head=1,tail=0,k=1;
dp[0]=0;
for(i=1;i<=n;i++)
{
if(head>tail)
{
head=tail=1;
q[head]=i;
}
else
{
while(head<=tail&&a[q[tail]]<=a[i])
tail--;
q[++tail]=i;
}
while(sum[i]-sum[k-1]>m)
k++;
while(head<=tail&&k>q[head])
head++;
dp[i]=dp[k-1]+a[q[head]];
for(j=head;j<tail;j++)
dp[i]=min(dp[i],dp[q[j]]+a[q[j+1]]);
}
if(flag)
printf("-1\n");
else
printf("%lld\n",dp[n]);
}
return 0;
}