## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## [Solution] Dog Walking Codeforces Solution | Codeforces Problem Solution 2022

D. Dog Walking
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point $0$ with your dog.

You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have some writings about how she ran. During the $i$-th minute, the dog position changed from her previous position by the value ${a}_{i}$ (it means, that the dog ran for ${a}_{i}$ meters during the $i$-th minute). If ${a}_{i}$ is positive, the dog ran ${a}_{i}$ meters to the right, otherwise (if ${a}_{i}$ is negative) she ran ${a}_{i}$ meters to the left.

During some minutes, you were chatting with your friend, so you don't have writings about your dog movement during these minutes. These values ${a}_{i}$ equal zero.

You want your dog to return to you after the end of the walk, so the destination point of the dog after $n$ minutes should be $0$.

Now you are wondering: what is the maximum possible number of different integer points of the line your dog could have visited, if you replace every $0$ with some integer from $-k$ to $k$ (and your dog should return to $0$ after the walk)? The dog visits an integer point if she runs through that point or reaches in it at the end of any minute. Point $0$ is always visited by the dog, since she is initially there.

If the dog cannot return to the point $0$ after $n$ minutes regardless of the integers you place, print -1.

Input

The first line of the input contains two integers $n$ and $k$ ($1\le n\le 3000;1\le k\le {10}^{9}$) — the number of minutes and the maximum possible speed of your dog during the minutes without records.

The second line of the input contains $n$ integers ${a}_{1},{a}_{2},\dots ,{a}_{n}$ ($-{10}^{9}\le {a}_{i}\le {10}^{9}$), where ${a}_{i}$ is the number of meters your dog ran during the $i$-th minutes (to the left if ${a}_{i}$ is negative, to the right otherwise). If ${a}_{i}=0$ then this value is unknown and can be replaced with any integer from the range $\left[-k;k\right]$.

Output

If the dog cannot return to the point $0$ after $n$ minutes regardless of the set of integers you place, print -1. Otherwise, print one integer — the maximum number of different integer points your dog could visit if you fill all the unknown values optimally and the dog will return to the point $0$ at the end of the walk.