cf978E Bus Video System

 

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.html

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.c++

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.ide

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).ui

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.atom

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.spa

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.orm

Examples

input
3 5
2 1 -3
output
3
input
2 4
-1 1
output
4
input
4 10
2 4 1 2
output
2

Note

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

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

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

題解:

只須要從後向前找出人最多的時刻,和最少的時刻。

#include <bits/stdc++.h>
using namespace std;
const int MAXN=200010;
const int INF=0x3f3f3f3f;
int sum[MAXN];

int main()
{
    int n,w,a;
    scanf("%d%d",&n,&w);
    int MAX=0,MIN=INF;
    for (int i = 1; i <=n ; ++i) {
        scanf("%d",&a);
        sum[i]=sum[i-1]+a;
        MAX=max(MAX,sum[i]);
        MIN=min(MIN,sum[i]);
    }
    MAX=w-MAX;
    MIN=MIN>0?0:-MIN;
    if(MAX-MIN+1<0) printf("0\n");
    else printf("%d\n",MAX-MIN+1);

    return 0;
}
相關文章
相關標籤/搜索