Stepping on Tiles
InterviewTime limit1sMemory limit256 MB
Given N distinct increasing numbers, find the maximum sum of a 3-or-more element arithmetic subsequence with any common difference, or 0 if none exists.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Hash map, Math, Array
- Solved
- No attempts yet
Problem
On the way to the competition venue, tiles are laid out in a single row, each marked with a natural number. All of the numbers are distinct, and they are arranged in increasing order from the beginning of the row to the end.
Cheolsu wants to start on one of the tiles and step across several tiles to reach the venue. He steps so that the numbers on the tiles he steps on increase by a fixed natural number () each time; that is, the numbers he steps on must form an arithmetic progression with common difference .
Depending on the starting tile and the common difference , the number of tiles he can step on in a row changes. Cheolsu wants to know the maximum possible sum of the numbers on the tiles he steps on. The run must consist of at least tiles.
For example, suppose the numbers on the tiles are:
1, 2, 6, 7, 11, 12, 13, 15, 17, 20, 23
Then every way to step on or more tiles in a row is listed in the table below.
The largest sum among them is (), whose sum is .
Given the numbers on the tiles in increasing order, write a program that finds the maximum sum of the numbers on tiles that can be stepped on in a run of or more, as described above. If no such run of or more tiles exists, print .
Input
The first line contains the number of tiles ().
The second line contains the natural numbers written on the tiles in increasing order, separated by spaces. Each number is at most , and all of them are distinct.
Output
If there is a run of or more tiles that can be stepped on, print on the first line the maximum sum of the numbers on such a run. If no such run exists, print .