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. 洗礼灵魂,修炼python(90)-- 知识拾遗篇 —— 协程

    协程 1.定义 协程,顾名思义,程序协商着运行,并非像线程那样争抢着运行.协程又叫微线程,一种用户态轻量级线程.协程就是一个单线程(一个脚本运行的都是单线程) 协程拥有自己的寄存器上下文和栈.协程调度 ...

  2. 洗礼灵魂,修炼python(68)--爬虫篇—番外篇之webbrowser模块

    题外话: 爬虫学到这里,我想你大部分的网站已经不再话下了对吧?有检测报文头的,我们可以伪造报文头为浏览器,有检测IP,我们可以用代理IP,有检测请求速度的,我们可以用time模块停顿一下,需要登录验证 ...

  3. MySQL多表更新的一个坑

    简述 MySQL支持update t1,t2 set t1.a=2;这种语法,别的关系数据库例如oracle和sql server都不支持.这种语法有时候写起来挺方便,但他有一个坑. 测试脚本 dro ...

  4. ELK 处理 Percona 审计日志(填坑)

    前提 1.有强烈的审计需求. 2.能允许10%-15%左右的性能损失. 3.有强烈的对数据库操作实时查看需求(一般都是为了领导要求). Logstash 比较坑的配置   1 2 3 4 5 6 7 ...

  5. IE6浏览器无法打开QQ邮箱

    原因:未启用TLS1.0 解决方法: 打开IE浏览器,依次打开 [Internet]→[高级],在 设置 选项卡中,勾选[使用TLS1.0],然后点击[确定]保存修改,重启浏览器即可.

  6. 持续集成-Jenkins常用插件安装

    1. 更新站点修改 由于之前说过,安装Jenkins后首次访问时由于其他原因[具体未知]会产生离线问题.网上找了个遍还是不能解决,所以只能跳过常用插件安装这步.进入Jenkins后再安装这些插件. 在 ...

  7. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  8. C#基础知识之泛型集合转换为DataTable

    在做项目中,遇到了将集合转换为DataTable的使用,在网上看了资料,在这里记录下来,分享. using System; using System.Collections.Generic; usin ...

  9. cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. Rancher2-----了解什么是rancher以及简单部署

    个人理解:就是相当于openstack的图形化界面,或者说应用程序的图形化界面,rancher功能就是在图形化界面去管理容器,包括运行容器,创建网络,存储等:rancher有个应用商店,可以根据自己的 ...