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 a_{i} occurs. If a_{i} > 0, then a_{i} bourles are deposited to Luba's account. If a_{i} < 0, then a_{i} bourles are withdrawn. And if a_{i} = 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 a_{i} = 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 ≤ 10^5, 1 ≤ d ≤ 10^9) —the number of days and the money limitation.

The second line contains n integer numbers a_1, a_2, ... a_{n} ( - 10^4 ≤ a_{i} ≤ 10^4), where a_{i} 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-----
Input
5 10
-1 5 0 -5 3

Output
0

Input
3 4
-10 0 20

Output
-1

Input
5 10
-5 0 10 -11 0

Output
2

Difficulty: interview

Starter code:

Solutions:
n, d = map(int, input().split())
line = list(map(int, input().split()))
pref = [0] * n
maxx = 0
for i in range(n):
    pref[i] = pref[max(i - 1, 0)] + line[i]
    maxx = max(maxx, pref[i])
maxr = [0] * n
for i in range(n - 1, -1, -1):
    if i == n - 1:
        maxr[i] = pref[i]
    else:
        maxr[i] = max(maxr[i + 1], pref[i])
sm = 0
bon = 0
ans = 0
b = True
if maxx > d:
    b = False
for i in range(n):
    elem = line[i]
    sm += elem
    if elem == 0:
        #print(sm, bon)
        if sm + bon < 0:
            ans += 1
            bon += max(0, d - (maxr[i] + bon))
        if sm + bon < 0:
            b = False
            break
    if sm + bon > d:
        b = False
        break
if b == False:
    print(-1)
else:
    print(ans)

n,d=map(int,input().split())
a=list(map(int,input().split()))
f=True
b=[a[0]]
for i in range(1,n):
    b.append(b[i-1]+a[i])
if max(b)>d:
    f=False
        
h=[0]*n
h[n-1]=b[n-1]
for i in range(n-2,-1,-1):
    h[i]=max(b[i],h[i+1])
x,k=0,0
for i in range(n):
    if a[i]==0 and b[i]+x<0:
        k+=1
        x+=d-(h[i]+x)
        if b[i]+x<0:
            f=False
            break
if f:
    print(k)
else:
    print(-1)

n, d = [int(x) for x in input().split()]
tr = [int(x) for x in input().split()]
cash = 0
flag = False
gr = []
for i in tr:
    if i != 0:
        cash += i
    gr.append(cash)
    if cash > d:
        flag = True
        break
if flag:
    print(-1)
else:
    mx = [-1] * n
    mx[-1] = gr[-1]
    for i in range(n - 2, -1, -1):
        mx[i] = max(gr[i], mx[i + 1])
    acash = 0
    count = 0
    for i in range(n):
        if tr[i] == 0:
            if gr[i] + acash < 0:
                acash += (d - mx[i] - acash)
                if gr[i] + acash < 0:
                    count = -1
                    break
                count += 1
    print(count)

n, d = map(int, input().split())
a = list(map(int, input().split()))
pref = [0 for i in range(n)]
c = 0
for i in range(n):
    c += a[i]
    if a[i] == 0:
        c = max(0, c)
    pref[i] = c
suff = [0 for i in range(n)]
suff[-1] = pref[-1]
mc = suff[-1]
for i in range(n-2, -1, -1):
    suff[i] = max(mc, pref[i])
    mc = suff[i]
    if a[i] == 0 and i > 0:
        mc = pref[i-1]
if max(suff) > d:
    print(-1)
    return
c = 0
ans = 0
for i in range(n):
    if a[i] != 0:
        c += a[i]
    else:
        if c < 0:
            ans += 1
            c = max(0, c)
            c += d - suff[i]
print(ans)

# -*- coding: utf-8 -*-

import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys

"""
created by shhuan at 2017/11/24 00:20

"""

N, D = map(int, input().split())
A = [int(x) for x in input().split()]

presum = [0] * (N+1)

for i in range(1, N+1):
    presum[i] = presum[i-1] + A[i-1]

rightmax = [0] * N
mx = float('-inf')
for i in range(N-1, -1, -1):
    mx = max(mx, presum[i+1])
    rightmax[i] = mx

# print(presum)
# print(rightmax)

if any(v > D for v in presum):
    print(-1)
    return

ans = 0
added = 0
for i in range(N):
    if A[i] == 0:
        if presum[i+1] + added < 0:
            dmin = 0-presum[i+1]-added
            dmax = min(D-presum[i+1]-added, D-added-rightmax[i])
            if dmin > dmax:
                print(-1)
                return

            added += dmax
            ans += 1
print(ans)

n, d = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
b = [0] * (n + 2)
b[n] = a[n]
now = a[n]
for i in range(n - 1 , 0 , -1):
    now = a[i] + max(now, 0)
    b[i] = now
now = 0
res = 0
for i in range(1 , n + 1):
    if a[i] == 0:
        if now < 0:
            res += 1
            now = min(d, max(0, d - b[i + 1]))
    else:
        now += a[i]
        if now > d:
            res = -1
            break
print(res)




f = lambda: map(int, input().split())
n, d = f()
h = s = k = 0
for q in f():
    h, s = h + q, min(d, s + q)
    if h > d:
        k = -1
        break
    if q == 0:
        h = max(0, h)
        if s < 0: s, k = d, k + 1
print(k)

def main():
	n, d = map(int, input().split())
	a = list(map(int, input().split()))
	
	pref, mx, add, ans = [0] * n, [0] * n, 0, 0

	for pos in range(n):
		pref[pos] = a[pos] if not pos else a[pos] + pref[pos-1]

	for pos in range(n-1, -1, -1):
		mx[pos] = pref[pos] if pos == n - 1 else max(mx[pos + 1], pref[pos])

	for i in range(n):
		if pref[i] + add > d:
			print("-1")
			return
		if a[i] == 0 and pref[i] + add < 0:
			ans += 1
			add += max(-(pref[i] + add), d - mx[i] - add)
	print(ans)

def __starting_point():
	main()
__starting_point()

H,L,t=0,0,0
n,d=map(int,input().split())
for i in map(int,input().split()):
    if i==0:
        if H<0:H=d;t+=1
        L=max(L,0)
    L+=i
    H=min(d,H+i)
    if L>d:print(-1);return()
print(t)

[n, d] = [int(x) for x in input().split(' ')]
A = [int(a) for a in input().split(' ')]

def solve():
    ans = 0
    bal = 0
    minGap = 0
    for i in range(n):
        if A[i] == 0:
            if bal < 0:
                go = min(-bal, minGap)
                minGap -= go
                bal += go
                if bal < 0:
                    ans += 1
                    bal = 0
                    minGap = d
        else:
            bal += A[i]
            if bal > d:
                return -1
            minGap = min(minGap, d - bal)
    return ans

print(solve())

n, d = list(map(int, input().split()))
l = list(map(int, input().split()))
mus = [0] * n
mus[0] = l[0]
cnt = 0
ans = 0
for i in range(1, n):
    mus[i] = mus[i - 1] +  l[i]
suf = [0] * n
suf[-1] = mus[-1]
for i in range(n - 2, -1, -1):
    suf[i] = max(mus[i], suf[i + 1])
for i in range(n):
    if l[i] == 0 and mus[i] + cnt < 0:
        if (d - suf[i] - cnt < 0 or d - suf[i] < abs(mus[i])):
            print(-1)
            return
        else:
            cnt += (d - suf[i]  - cnt)
            ans += 1
if suf[0] > d:
    print(-1)
    return
print(ans)

#Bhargey Mehta (Sophomore)
#DA-IICT, Gandhinagar
import sys, math, queue, bisect
#sys.stdin = open("input.txt", "r")
MOD = 10**9+7
sys.setrecursionlimit(1000000)

n, d = map(int, input().split())
a = list(map(int, input().split()))
p = [0 for i in range(n)]
for i in range(n):
    p[i] = p[i-1]+a[i]
mx = [-1 for i in range(n)]
mx[-1] = p[-1]
for i in range(n-2, -1, -1):
    mx[i] = max(mx[i+1], p[i])
c = 0
ans = 0
for i in range(n):
    p[i] += c
    if p[i] > d:
        print(-1)
        return
    if a[i] != 0 or p[i] >= 0: continue

    av = d-(mx[i]+c)
    if -p[i] > av:
        print(-1)
        return
    ans += 1
    c = d-mx[i]
print(ans)

import os
import sys

def log(*args, **kwargs):
    if os.environ.get('CODEFR'):
        print(*args, **kwargs)


#n = int(input())
n, d = tuple(map(int,input().split()))
a = list(map(int,input().split()))

s = 0
k = 0
dep = 0
for i in a:
    if i == 0:
        if s < 0:
            if dep < -s:
                k += 1
                dep = d
            else:
                dep += s
            s = 0
    else:
        s += i
        if s > d:
            print(-1)
            return
        dep = min(dep, d - s)

print(k)


R = lambda: map(int, input().split())
n, k = R()
arr = list(R())
tup = [0, 0]
res = 0
for x in arr:
    if x != 0:
        tup[0], tup[1] = tup[0] + x, tup[1] + x
        tup[1] = min(tup[1], k)
    elif tup[1] < 0:
        tup[0], tup[1] = 0, k
        res += 1
    else:
        tup[0] = max(0, tup[0])
    if tup[0] > k:
        res = -1
        break
print(res)

H,L,t=0,0,0
n,d=map(int,input().split())
for i in map(int,input().split()):
    if i==0:
        if H<0:H=d;t+=1
        L=max(L,0)
    L+=i
    H=min(d,H+i)
    if L>d:print(-1);return()
print(t)

import sys
input = sys.stdin.readline

n, d = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
ub, lb = 0, 0

for x in a:
    if x == 0:
        if ub < 0:
            ub, lb = d, 0
            ans += 1
        if lb < 0:
            lb = 0
    else:
        ub = min(d, ub+x)
        lb += x
        if lb > d:
            print(-1)
            return

print(ans)


n,d=list(map(int,input().split()))
a=list(map(int,input().split()))
s=0
m=0
ans=0 
flag=True
n=len(a)
for i in range(n):
    if a[i]==0:
        if s<0:
            s=d
            m=d
            ans+=1
        else:
            m=min(m,s)
    elif a[i]<0:
        s=s+a[i] 
    else:
        if(s+a[i]>d):
            if(s+a[i]-d)>m:
                flag=False
                break 
            else:
                m-=(s+a[i]-d)
                s=d 
        else:
            s=s+a[i] 
if flag:
    print(ans)
else:
    print(-1)

import sys

n, d = list(map(int, input().split()))
a = list(map(int, input().split()))
ub, lb = 0, 0
ans = 0

for x in a:
    if x == 0:
        if ub < 0:
            ub, lb = d, 0
            ans += 1
        lb = max(lb, 0)
    else:
        ub = min(d, ub+x)
        lb += x
        if lb > d:
            print(-1)
            return
print(ans)