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 y−x. So the system records show how number of passengers changed.

The test run was made for single bus and n 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 w passengers inclusive).

Input

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

The second line contains a sequence i-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 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 2 passengers.

In the second example initially in the bus could be 4 passengers.

In the third example initially in the bus could be 1 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\n");
        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;
}

  

相关文章: