E. Bus Video System

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The busses in Berland are equipped with a video surveillance system. The system records information about changes in the number of passengers in a bus after stops.

If xx is the number of passengers in a bus just before the current bus stop and yy is the number of passengers in the bus just after current bus stop, the system records the number yxy−x. So the system records show how number of passengers changed.

The test run was made for single bus and nn bus stops. Thus, the system recorded the sequence of integers a1,a2,,ana1,a2,…,an (exactly one number for each bus stop), where aiai is the record for the bus stop ii. The bus stops are numbered from 11 to nn in chronological order.

Determine the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww (that is, at any time in the bus there should be from 00 to ww passengers inclusive).

Input

The first line contains two integers nn and w(1n1000,1w109)(1≤n≤1000,1≤w≤109) — the number of bus stops and the capacity of the bus.

The second line contains a sequence a1,a2,,ana1,a2,…,an (106ai106)(−106≤ai≤106), where aiai equals to the number, which has been recorded by the video system after the ii-th bus stop.

Output

Print the number of possible ways how many people could be in the bus before the first bus stop, if the bus has a capacity equals to ww. If the situation is contradictory (i.e. for any initial number of passengers there will be a contradiction), print 0.

Examples
input
Copy
3 5
2 1 -3
output
Copy
3
input
Copy
2 4
-1 1
output
Copy
4
input
Copy
4 10
2 4 1 2
output
Copy
2
Note

In the first example initially in the bus could be 00, 11 or 22 passengers.

In the second example initially in the bus could be 11, 22, 33 or 44 passengers.

In the third example initially in the bus could be 00 or 11 passenger.

维护一个最小值和最大值。然后求上下界的交。

#include <iostream>
#include <bits/stdc++.h>
#define maxn 1005
using namespace std;
int a[maxn]={0};
int main()
{
    int n,w,i;
    scanf("%d%d",&n,&w);

    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int sum=0;
    int minum=1e9+5;
    int maxim=0;
    for(i=1;i<=n;i++)
    {   minum=min(minum,sum);
        maxim=max(maxim,sum);
        sum+=a[i];
    }
    minum=min(0,min(minum,sum));
    maxim=max(0,max(maxim,sum));
    if(abs(minum)>w||abs(maxim)>w)
    {
        printf("0
");
        return 0;
    }
    else
    {
       int l1=abs(minum);
       int r2=abs(w-maxim);
       int ans=r2-l1+1;
       if(ans<0) ans=0;
       cout<<ans<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/zyf3855923/p/9039800.html