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;
}
时间: 2024-07-30 08:54:00

code force 893D Credit Card的相关文章

codeforces 893D Credit Card 贪心 思维

codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会给卡充入\(a_i\)元 2.\(a_i<0\) 银行从卡中扣除\(a_i\)元 3.\(a_i=0\) 银行对你的卡进行评估,违背了规则就无权再使用此卡 规则1:卡内的余额不得超过\(d\)元 规则2:当\(a_i=0\)时,卡内的余额不能是负数 现在问为了维持信用的平衡,最少去银行几次.(去一次,充

magento 开启 3D secure credit card validation

因为国外盗刷严重,于是得开启验证. 首先可以去 https://developer.cardinalcommerce.com/try-it-now.shtml.这上面有测试账号,截图如下: 这上面的信息填到 system/configuration/paymentServices/ 里面 接着去paymentMethod 下配置paypal的credit card: ok.可以去前台下个单试试,是不是最后出来这个验证页面了 上面的是测试配置.生产环境配置差不多,想要注册自己的账号,可以去 htt

(原创)北美信用卡(Credit Card)个人使用心得与总结(个人理财版) [精华]

http://forum.chasedream.com/thread-766972-1-1.html 本人2010年 8月F1 二度来美,现在credit score 在724-728之间浮动,最高的时候也就730,属于good range, 大于746的才算excellent score.两年之内从没有信用记录到Good,觉得还算不错,现在把自己如何build credit history和一些心得与大家分享.希望对大家有帮助. 信用记录在北美的重要性自然不必多说.我的启蒙是校内/renren

Real Time Credit Card Fraud Detection with Apache Spark and Event Streaming

https://mapr.com/blog/real-time-credit-card-fraud-detection-apache-spark-and-event-streaming/ Editor's Note: Have questions about the topics discussed in this post? Search for answers and post questions in the Converge Community. In this post we are

code force 2B The least round way

There is a square matrix n?×?n, consisting of non-negative integer numbers. You should find such a way on it that starts in the upper left cell of the matrix; each following cell is to the right or down from the current cell; the way ends in the bott

code force 798cMike and gcd problem

Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he c

Computop支付网关(一) credit Card

1.界面没有中文,只能选择英文 sp – Spanish; en – English; ca – Catalan; fr – French; de – German; du – Dutch; it – Italian; se – Swedish; pt– Portuguese; pl – Polish; gl – Galician; eu – Basque 2. 返回的urlSuccess,urlFail 必须是SSL的,就算我指定http,不是https,它还是返回https

code force #419(div2)C. Karen and Game

On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of choosing one row or

Code Force 429B Working out【递推dp】

Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and mcolumns. Let number a[i][j] represents the calories burned by performing workout at the