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 y−xy−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 aiaiis 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 ww (1≤n≤1000,1≤w≤109)(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 (−106≤ai≤106)(−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
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.

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.

题意:

给你一个含有n个整数的数组,每一个数a[i]代表汽车在站i时,车上增多了a[i]个人,如果a[i]为负,代表减少了人数。

并告诉你这个汽车的最大承载力为w个人,

请你判断初始时汽车上有多少个人,才满足整个数组的情况,。

如果某一个情况,车上的人数为负,或者人数大于w,那么说明这个数组时不合理的,。这时请输出0

思路:

可以抽象为,求这个数组的前缀和数组中的最大值和最小值,。只要最大值不大于容量,再判断下最低值的绝对值不大于容量。就可以说明是合理的。

然后可以的方案数中初始的人数一定是连续的,那么这些人数中的最大值是min(w-maxsum,w) ,即不让过程中容量大于w的最大值。

最小值是max(0,-1*minsum),然后最大值减去最小值+1就是答案了。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
// HFUU-QerM
// 21:49:59
ll n;
ll w;
ll a[maxn];
int main()
{
//freopen("D:\common_text\code_stream\in.txt","r",stdin);
//freopen("D:\common_text\code_stream\out.txt","w",stdout);
gbtb;
cin >> n >> w;
repd(i, , n)
{
cin >> a[i];
}
ll f = -1e18;
ll g = 1e18;
ll v = 0ll;
repd(i, , n)
{
v += a[i];
f = max(f, v);
g = min(g, v);
}
// db(f);
// db(g);
if ((abs(f)) > w || abs(g) > w)
{
cout << << endl;
} else
{
ll s = w - f;
ll x = 0ll;
// db(s);
s=min(s,w);
if (g < )
{
x = - * g;
}
// db(x);
if (x > s)
{
cout << << endl;
} else
{
cout << s - x + 1ll << endl;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Bus Video System CodeForces - 978E (思维)的更多相关文章

  1. cf978E Bus Video System

    The busses in Berland are equipped with a video surveillance system. The system records information ...

  2. Codeforces 978E:Bus Video System

    题目链接:http://codeforces.com/problemset/problem/978/E 题意 一辆公交车,在每站会上一些人或下一些人,车的最大容量为w,问初始车上可能有的乘客的情况数. ...

  3. CF978E Bus Video System【数学/前缀和/思维】

    [链接]: CF [分析]: 设上车前人数 x ,中途最大人数为 x+max ,最小人数为 x+min (max≥0,min≤0) 可得不等式组 x+max≤w, x+min≥0 整数解个数为 max ...

  4. pygame.error: video system not initialized

    在pygame写游戏出现pygame.error: video system not initialized 源代码 import sysimport pygamedef run_game(): py ...

  5. Codeforces 424A (思维题)

    Squats Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  6. CodeForces - 417B (思维题)

    Crash Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  7. Error Correct System CodeForces - 527B

    Ford Prefect got a job as a web developer for a small company that makes towels. His current work ta ...

  8. Codeforces 1060E(思维+贡献法)

    https://codeforces.com/contest/1060/problem/E 题意 给一颗树,在原始的图中假如两个点连向同一个点,这两个点之间就可以连一条边,定义两点之间的长度为两点之间 ...

  9. Queue CodeForces - 353D (思维dp)

    https://codeforces.com/problemset/problem/353/D 大意:给定字符串, 每一秒, 若F在M的右侧, 则交换M与F, 求多少秒后F全在M左侧 $dp[i]$为 ...

随机推荐

  1. SQL Server Browser探究

    一.官网关于SQL SERVER Browser服务的解释(谷歌翻译后稍作修改的): https://docs.microsoft.com/en-us/sql/tools/configuration- ...

  2. c/c++ static关键字

    static关键字 1,static 成员变量 static 成员变量不随着对象的创建而开辟内存空间.也就是说,不管从哪个对象去看static成员变量,都是一样的. 2, static 成员方法 st ...

  3. 我的BRF+自学教程(一):公式(formula)

    Business Rule Framework Plus(业务规则框架,以下简称BRFplus或BRF+)是一个强大的工具, 它允许用户以直观的方式对规则建模,并在不同的应用程序中重用这些规则.通过它 ...

  4. spring的工厂类

    主要介绍两种工厂接口BeanFactory(老版本,已过时)和ApplicationContext ApplicationContext接口:每次在加载applicationContext.xml的时 ...

  5. TensorFlow——循环神经网络基本结构

    1.导入依赖包,初始化一些常量 import collections import numpy as np import tensorflow as tf TRAIN_DATA = "./d ...

  6. 转://Linux下误删除/home目录的恢复方法

    一般情况下,我们在安装Oracle数据库的时候,都会创建一个Oracle用户,用该用户来安装和管理Oracle.Oracle用户的根目录就是/home/oracle. 通常安装Oracle数据库是按照 ...

  7. 【转】js中通过docment.cookie获取到的内容不完整! 在浏览器的application里的cookie里可以看到完整的cookie,个别字段无法通过document.cookie获取。 是否有其他办法可以获取到??

    js中通过docment.cookie获取到的内容不完整!在浏览器的application里的cookie里可以看到完整的cookie,个别字段无法通过document.cookie获取.是否有其他办 ...

  8. go中rune和byte的用处

    参考:https://www.jianshu.com/p/4fbf529926ca rune是用来区分字符值和整数值的 byte 等同于int8,即一个字节长度,常用来处理ascii字符 rune 等 ...

  9. 数据库迁移之mysql-redis.txt

    一.mysql迁移数据到redis 关于redis+mysql应用: 微博当然是最大的redis集群了: 总结了基本流程: 1. 发微博– > 进入消息队列– > 存入MySQL– > ...

  10. fastJson 之 JSONPath使用

    1. JSONPath介绍 官网地址: https://github.com/alibaba/fastjson/wiki/JSONPath fastjson 1.2.0之后的版本支持JSONPath. ...