Credit Card

time limit per test2 seconds

memory limit per test256 megabytes

Recenlty Luba got a credit card and started to use it. Let's consider n consecutive days Luba uses the card.

She starts with 0 money on her account.

In the evening of i-th day a transaction ai occurs. If ai > 0, then ai bourles are deposited to Luba's account. If ai < 0, then ai bourles are withdrawn. And if ai = 0, then the amount of money on Luba's account is checked.

In the morning of any of n days Luba can go to the bank and deposit any positive integer amount of burles to her account. But there is a limitation: the amount of money on the account can never exceed d.

It can happen that the amount of money goes greater than d by some transaction in the evening. In this case answer will be «-1».

Luba must not exceed this limit, and also she wants that every day her account is checked (the days when ai = 0) the amount of money on her account is non-negative. It takes a lot of time to go to the bank, so Luba wants to know the minimum number of days she needs to deposit some money to her account (if it is possible to meet all the requirements). Help her!

Input

The first line contains two integers n, d (1 ≤ n ≤ 105, 1 ≤ d ≤ 109) —the number of days and the money limitation.

The second line contains n integer numbers a1, a2, ... an ( - 104 ≤ ai ≤ 104), where ai represents the transaction in i-th day.

Output

Print -1 if Luba cannot deposit the money to her account in such a way that the requirements are met. Otherwise print the minimum number of days Luba has to deposit money.

Examples

inputCopy

5 10

-1 5 0 -5 3

outputCopy

0

inputCopy

3 4

-10 0 20

outputCopy

-1

inputCopy

5 10

-5 0 10 -11 0

outputCopy

2





找了一个写的比较好的中文题面QAQ。。。

有一张信用卡可以使用,每天白天都可以去给卡充钱。到了晚上,进入银行对卡的操作时间,操作有三种:

1.ai>0 银行会给卡充入ai元

2.ai<0 银行从卡中扣除ai元

3.ai=0 银行对你的卡进行评估,违背了规则就无权再使用此卡

规则1:卡内的余额不得超过d元

规则2:当ai=0时,卡内的余额不能是负数

现在问为了维持信用的平衡,最少去银行几次。(去一次,充一次钱)



反正看各种大神的算法感觉特别厉害。。。

我自己yy了一个乱七八糟的东西,运气好就1A了233


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, d, ans, a[maxn], b[maxn], c[maxn];
vector<int> lpl; inline void check()
{
for(int i = 1; i <= n; ++i) printf("%d ", a[i]); printf("\n");
for(int i = 1; i <= n; ++i) printf("%d ", b[i]); printf("\n");
for(int i = 1; i <= n; ++i) printf("%d ", c[i]); printf("\n");
} inline void prework()
{
int sum = 0;
for(int i = 1; i <= n; ++i){
if(a[i] == 0){
lpl.push_back(i);
if(sum < 0){b[i] = -sum; sum = 0;}
c[i] = sum;
continue;
}
sum += a[i]; if(sum > d){printf("-1"); exit(0);}
}
//check();
} inline void workk()
{
ans = lpl.size();
for(int i = lpl.size() - 1; i >= 1; --i){
int now = lpl[i], pre = lpl[i - 1], sum = c[pre], mx = sum;
for(int j = pre + 1; j < now; ++j)
{sum += a[j]; mx = max(mx, sum);}
if(mx + b[now] <= d){ans--; b[pre] += b[now];}
}
if(b[1] == 0) ans--; cout << ans;
} int main()
{
scanf("%d%d", &n, &d); n++; a[1] = 0;
for(int i = 2; i <= n; ++i) scanf("%d", &a[i]);
prework(); workk();
return 0;
}

Educational Codeforces Round 33 D. Credit Card的更多相关文章

  1. Educational Codeforces Round 33 (Rated for Div. 2) D. Credit Card

    D. Credit Card time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. Educational Codeforces Round 33

    # Who = Penalty * A B C D E F 479 arkethos 4 247   + 00:08 + 00:19 +1 00:59 +2 01:41     479  ne-leo ...

  3. Educational Codeforces Round 33 (Rated for Div. 2) D题 【贪心:前缀和+后缀最值好题】

    D. Credit Card Recenlty Luba got a credit card and started to use it. Let's consider n consecutive d ...

  4. 【Educational Codeforces Round 33 D】Credit Card

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每次遇到0的时候,看看当前累计的delta是多少. 如果大于0,则temp = d-delta; 小于0,取temp2 = min( ...

  5. Educational Codeforces Round 33 (Rated for Div. 2) E. Counting Arrays

    题目链接 题意:给你两个数x,yx,yx,y,让你构造一些长为yyy的数列,让这个数列的累乘为xxx,输出方案数. 思路:考虑对xxx进行质因数分解,设某个质因子PiP_iPi​的的幂为kkk,则这个 ...

  6. Educational Codeforces Round 33 (Rated for Div. 2) F. Subtree Minimum Query(主席树合并)

    题意 给定一棵 \(n\) 个点的带点权树,以 \(1\) 为根, \(m\) 次询问,每次询问给出两个值 \(p, k\) ,求以下值: \(p\) 的子树中距离 \(p \le k\) 的所有点权 ...

  7. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  8. Educational Codeforces Round 33 (Rated for Div. 2)A-F

    总的来说这套题还是很不错的,让我对主席树有了更深的了解 A:水题,模拟即可 #include<bits/stdc++.h> #define fi first #define se seco ...

  9. Educational Codeforces Round 33 (Rated for Div. 2) C. Rumor【并查集+贪心/维护集合最小值】

    C. Rumor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

随机推荐

  1. socket - Linux 套接字

    总览 #include <sys/socket.h> mysocket = socket(int socket_family, int socket_type, int protocol) ...

  2. openstack--all-in-one部署

    安装过程 本次宿主机(即安装OpenStack的机器)的操作系统是CentOS 7.5.安装的OpenStack是目前最新的rocky版本,官方文档建议机器至少有16 GB的内存,处理器硬件虚拟化扩展 ...

  3. vscode舒适的字体风格

    首选项-->设置-->输入setting.json-->查找到设置文件 添加如下配置 "editor.tabSize": 2, "files.assoc ...

  4. 第03章 AOP前奏

    第03章 AOP前奏 提出问题 ●情景:数学计算器 ●要求 ①执行加减乘除运算 ②日志:在程序执行期间追踪正在发生的活动 ③验证:希望计算器只能处理正数的运算 ●常规实现 ●问题 ○代码混乱:越来越多 ...

  5. CONNECT_BY_ROOT

    1.select * from  EMP t  where t.deptno = 10;   EMPNO     ENAME     JOB     MGR     HIREDATE     SAL  ...

  6. python模块之numpy,pandas基本用法

    numpy: 是 Python 的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库简单来说:就是支持一维数组和多维数组的创建和操作,并有丰富的函数库. 直接看例子 ...

  7. JAVA 利用 jmc或jvisualvm 监控 本地或者远程JVM

    本地检测之间到$JAVA_HOME/bin 下的目录点击jmc 或者jvisualvm,然后选择你要监控的app 可是一般我们生产环境项目都是部署在远程,这个时候想要监控怎么办 1.监控tomcat ...

  8. 使用burpsuite的几点经验

    背景: 最近在做完一个系统的渗透测试,我同事在我昨晚渗透测试之后上去看了一下,发现了了一个我没有发现的漏洞. 请教了他之后,发现这个漏洞他是通过burp的scan发现的,只是对一个有参数的请求包sca ...

  9. ConcurrentLinkedQueue和LinkedBlockingQueue区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11394436.html 线程安全队列类图 两者的区别在于 ConcurrentLinkedQueue基 ...

  10. 前端-个人网页开发最常用的插件Superslide 与 swiper

    给初入前端的同学,安利2个轮播图插件 SuperSlide 与 Swiper ! SuperSlide PC端与移动端 的轮播图特效都是分开写2个js插件,而Swiper 基本一个插件可以写PC端与移 ...