A15145 | Dog Walking
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 visit on her way, 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.
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 visit on her way, 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.
输入格式
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, \ldots, 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 $[-k; k]$ .
The second line of the input contains $n$ integers $a_1, a_2, \ldots, 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 $[-k; k]$ .
输出格式
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.
输入输出样例
输入 #1
3 2 5 0 -4
输出 #1
6
输入 #2
6 4 1 -2 0 3 -4 5
输出 #2
7
输入 #3
3 1000000000 0 0 0
输出 #3
1000000001
输入 #4
5 9 -7 -3 8 12 0
输出 #4
-1
输入 #5
5 3 -1 0 3 3 0
输出 #5
7
输入 #6
5 4 0 2 0 3 0
输出 #6
9
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted