CF BUDDY
← Problems·

1319B · Journey Planning

1400 · data structures, dp, greedy

Problem: Tanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn.

Tanya plans her journey as follows. First of all, she will choose some city c1c_1 to start her journey. She will visit it, and after that go to some other city c2>c1c_2 > c_1, then to some other city c3>c2c_3 > c_2, and so on, until she chooses to end her journey in some city ck>ck1c_k > c_{k - 1}. So, the sequence of visited cities [c1,c2,,ck][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 ii has a beauty value bib_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 cic_i and ci+1c_{i + 1}, the condition ci+1ci=bci+1bcic_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.

For example, if n=8n = 8 and b=[3,4,4,6,6,7,8,9]b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:

  • c=[1,2,4]c = [1, 2, 4];
  • c=[3,5,6,8]c = [3, 5, 6, 8];
  • c=[7]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?

Input Format: The first line contains one integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of cities in Berland.

The second line contains nn integers b1b_1, b2b_2, ..., bnb_n (1bi41051 \le b_i \le 4 \cdot 10^5), where bib_i is the beauty value of the ii-th city.

Output Format: Print one integer — the maximum beauty of a journey Tanya can choose.

Note: The optimal journey plan in the first example is c=[2,4,5]c = [2, 4, 5].

The optimal journey plan in the second example is c=[1]c = [1].

The optimal journey plan in the third example is c=[3,6]c = [3, 6].

Sample Cases

Case 1

Input

6
10 7 1 9 10 15

Output

26

Case 2

Input

1
400000

Output

400000

Case 3

Input

7
8 9 26 11 12 29 14

Output

55

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