CF BUDDY
← Problems·

1680D · Dog Walking

2400 · brute force, greedy, math

Problem: 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 00 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 ii-th minute, the dog position changed from her previous position by the value aia_i (it means, that the dog ran for aia_i meters during the ii-th minute). If aia_i is positive, the dog ran aia_i meters to the right, otherwise (if aia_i is negative) she ran aia_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 aia_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 nn minutes should be 00.

Now you are wondering: what is the maximum possible number of different integer points of the line your dog could visit on her way, if you replace every 00 with some integer from k-k to kk (and your dog should return to 00 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 00 is always visited by the dog, since she is initially there.

If the dog cannot return to the point 00 after nn minutes regardless of the integers you place, print -1.

Input Format: The first line of the input contains two integers nn and kk (1n3000;1k1091 \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 nn integers a1,a2,,ana_1, a_2, \ldots, a_n (109ai109-10^9 \le a_i \le 10^9), where aia_i is the number of meters your dog ran during the ii-th minutes (to the left if aia_i is negative, to the right otherwise). If ai=0a_i = 0 then this value is unknown and can be replaced with any integer from the range [k;k][-k; k].

Output Format: If the dog cannot return to the point 00 after nn 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 00 at the end of the walk.

Sample Cases

Case 1

Input

3 2
5 0 -4

Output

6

Case 2

Input

6 4
1 -2 0 3 -4 5

Output

7

Case 3

Input

3 1000000000
0 0 0

Output

1000000001

Case 4

Input

5 9
-7 -3 8 12 0

Output

-1

Case 5

Input

5 3
-1 0 3 3 0

Output

7

Case 6

Input

5 4
0 2 0 3 0

Output

9

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback