A13326 | Journey Planning
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Tanya wants to go on a journey across the cities of Berland. There are $n$ cities situated along the main railroad line of Berland, and these cities are numbered from $1$ to $n$ .
Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$ , then to some other city $c_3 > c_2$ , and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$ . So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$ , the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold.
For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$ , there are several three possible ways to plan a journey:
- $c = [1, 2, 4]$ ;
- $c = [3, 5, 6, 8]$ ;
- $c = [7]$ (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Tanya plans her journey as follows. First of all, she will choose some city $c_1$ to start her journey. She will visit it, and after that go to some other city $c_2 > c_1$ , then to some other city $c_3 > c_2$ , and so on, until she chooses to end her journey in some city $c_k > c_{k - 1}$ . So, the sequence of visited cities $[c_1, c_2, \dots, c_k]$ should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city $i$ has a beauty value $b_i$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $c_i$ and $c_{i + 1}$ , the condition $c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$ must hold.
For example, if $n = 8$ and $b = [3, 4, 4, 6, 6, 7, 8, 9]$ , there are several three possible ways to plan a journey:
- $c = [1, 2, 4]$ ;
- $c = [3, 5, 6, 8]$ ;
- $c = [7]$ (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
输入格式
The first line contains one integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the number of cities in Berland.
The second line contains $n$ integers $b_1$ , $b_2$ , ..., $b_n$ ( $1 \le b_i \le 4 \cdot 10^5$ ), where $b_i$ is the beauty value of the $i$ -th city.
The second line contains $n$ integers $b_1$ , $b_2$ , ..., $b_n$ ( $1 \le b_i \le 4 \cdot 10^5$ ), where $b_i$ is the beauty value of the $i$ -th city.
输出格式
Print one integer — the maximum beauty of a journey Tanya can choose.
输入输出样例
输入 #1
6 10 7 1 9 10 15
输出 #1
26
输入 #2
1 400000
输出 #2
400000
输入 #3
7 8 9 26 11 12 29 14
输出 #3
55
The optimal journey plan in the first example is $c = [2, 4, 5]$ .
The optimal journey plan in the second example is $c = [1]$ .
The optimal journey plan in the third example is $c = [3, 6]$ .
The optimal journey plan in the second example is $c = [1]$ .
The optimal journey plan in the third example is $c = [3, 6]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted