code force 893D Credit Card

Posted 不积跬步无以至千里,不积小流无以成江海

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了code force 893D Credit Card相关的知识,希望对你有一定的参考价值。

Recenlty Luba got a credit card and started to use it. Let‘s consider n consecutive days Luba uses the card.

She starts with 0 money on her account.

In the evening of i-th day a transaction ai occurs. If ai?>?0, then ai bourles are deposited to Luba‘s account. If ai?<?0, then ai bourles are withdrawn. And if ai?=?0, then the amount of money on Luba‘s account is checked.

In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.

It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be ?-1?.

Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai?=?0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!

Input

The first line contains two integers n, d (1?≤?n?≤?105, 1?≤?d?≤?109) —the number of days and the money limitation.

The second line contains n integer numbers a1,?a2,?... an (?-?104?≤?ai?≤?104), where ai represents the transaction in i-th day.

Output

Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.

Example

Input
5 10
-1 5 0 -5 3
Output
0
Input
3 4
-10 0 20
Output
-1
Input
5 10
-5 0 10 -11 0
Output
2
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int n,d,a[100000+20];
int main()
{
    scanf("%d%d",&n,&d);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    int minn=0,maxx=0;//当前账户的最小值和最大值
    int ans = 0;
    for(int i=0;i<n;i++)
    {
        if(a[i]==0)
        {
            //如果最小值小于零,则将最小值重新置为零
            if(minn<0)minn=0;
            //如果最大值小于零,则将最大值置为最大额度,次数加一
            //表示第i天早上去了一次银行,并且将账户存到最大额度
            if(maxx<0){maxx=d;ans++;}
        }
        else
        {
            //每次都要更新当前账户的最值
            minn+=a[i];
            maxx+=a[i];
            if(minn>d)//如果最小值大于最大额度输出-1
            {
                printf("-1\n");
                return 0;
            }
            if(maxx>d)//如果最大值大于最大额度则将最大值置为最大额度
            {
                maxx=d;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

 

以上是关于code force 893D Credit Card的主要内容,如果未能解决你的问题,请参考以下文章

code forces 994C

code forces 979C

code forces 999CAlphabetic Removals

code forces431C_dp_注意结果要多加两个modd保证输出是正数

code forces 990C

code forces 994B