All problems

Problem 957

Best Sightseeing Pair

Medium · 20 pts · Array, Dynamic Programming

Problem

Sightseeing spot i has a value VALUES[i]. The score of a pair of spots i < j is VALUES[i] + VALUES[j] + i - j: their values minus the distance between them. Find the maximum score of any pair. Split the score into (VALUES[i] + i) + (VALUES[j] - j) and scan j from left to right, remembering the best VALUES[i] + i seen so far.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the maximum score.

Constraints

2 <= N <= 50000; 1 <= VALUES[i] <= 1000

Example input

5
8 1 5 2 6

Example output

11

Explanation

In 8 1 5 2 6, the pair (0, 2) scores 8 + 5 + 0 - 2 = 11, the best of all pairs.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Ways to Form a Target String Given a Dictionary Next: Maximum Alternating Subsequence Sum