Educational codeforces Round D. Credit Card

Source: Internet
Author: User
Tags integer numbers numeric value


Original question:
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 in her account.



In the evening of I-th day a transaction AI occurs. If ai > 0, then AI Bourles is deposited to Luba's account. If AI < 0, then AI Bourles is withdrawn. And if Ai = 0, then the amount of money on Luba's account is checked.



In the morning of a n days Luba can go to the bank and deposit any positive an integer amount of Burles to his account. But there was a limitation:the amount of money in the account can never exceed D.



It can happen the amount of money goes greater than D by some transaction in the evening. In this case answer would be«-1».



Luba must not exceed this limit, and also she wants that every day hers account was checked (the days when AI = 0) The Amoun T of money was 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 To his account (if it was possible to meet all the requirements). Help her!



Input
The first line contains-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 D Ay.



Output
Print-1 If Luba cannot deposit the money to his account in such a-the-the-the-requirements is met. Otherwise Print the minimum number of days Luba have to deposit money.



Examples
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



English:



You have an account, you can save and withdraw money, give you two numbers n and D. Indicates that there are n operations, and the maximum amount of money in the account.
Then give you the number of n, positive for saving, negative for the money, 0 for audit. The total amount of the deposit must not exceed D, and each audit will require an account to be more than 0. You can add money to your account every day, and now ask for at least how much money you can make to keep your account audited without overdraft.
If the daily fixed amount of deposit and withdrawal exceeds the upper limit, output-1.


#include<bits/stdc++.h>
using namespace std;
int res[100001];
int n,d;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>d)
    {
        for(int i=1;i<=n;i++)
            cin>>res[i];
        int l,r,cnt;
        l=r=cnt=0;
        int flag=1;
        for(int i=1;i<=n;i++)
        {
            if(res[i]==0)
            {
                if(r<0)
                {
                    cnt++;
                    r=d;
                }
                if(l<0)
                    l=0;
            }
            else
            {
                l+=res[i];
                r+=res[i];
                if(r>d)
                    r=d;
                if(l>d)
                {
                    flag=0;
                    break;
                }
            }
        }
        if(flag)
            cout<<cnt<<endl;
        else
            cout<<-1<<endl;
    }
    return 0;
}


Answer:



Because each time the input is 0 to check whether the account is owed, and save the number of unlimited. Therefore, you can consider every time you save money in the audit, here to meet three conditions. Every time you save, you have to pay for the money you owe. In the subsequent sequence of operations, the account cap does not exceed D, as much as possible to make the back save the least number of times.



For example
5 10
-5 0 10-11 0



The first condition, the owed money, because not limit the amount of money, so directly into a positive number even if it is a saving.



The second condition is more important, the current range of savings is how much to make the subsequent operation will not appear in the account of the amount of money not more than D.
Since the following questions are involved, it is certain that the following data will be taken into account and the subsequent data processed. I made a serious mistake when considering this problem, I put all the data in accordance with 0 of this operation separately considered, respectively, from the back to calculate the cumulative sum, the result is more trouble.



The official explanation is that if you want to keep a numeric value suf[i when you audit, this value records the maximum amount of accounts that will appear after the forward to I. So the current amount of money in the fortress is D-suf[i].
First, the calculation of summation and Pre[i] is calculated from beginning to end, and this step retains the amount of balance in the account from the beginning of the operation.
Then, from the back forward, calculate suf[i], each time it retains Suf[i]=max (Maxsuf[i + 1], Pre[i]), record the maximum amount of balance in the account from the back to the I-O account.
For example, this article



Find another solution when searching for solutions on the web. Each time you maintain an interval, which is the maximum remaining balance in the account when you do not save money, and the maximum balance R remaining after the bank saves money.
Each time the calculation, if found L greater than D, indicating that the original sequence of operations unreasonable, jump out.
Every time you check, if you find that r is less than 0, at this point, then directly to the balance of the account to D, if found L is less than 0, then l to 0, and enhance a deposit record. The reason why the
can do this is that if the current deposit should be accessed according to the following sequence, a value can be used to make the next balance exactly the upper limit, then there is no need to consider the current amount of money, only need to fill the money. The maximum remaining balance when not depositing must be at least guaranteed to be non-negative at the time of audit.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.