Under construction

Still being built. Things may change

Infinite Sequence

Time limit2sMemory limit1024 MB

Summary
Given array A, repeat it forever and subtract each index i from the i-th value. Find the largest sum of a contiguous segment.
Level

Medium7 of 10

Topics
Prefix sum, Math, Dynamic programming
Solved
No attempts yet

Problem

Sehun received a sequence AA of NN integers as a birthday gift. Finding the sequence too short, he decided to build an infinitely long sequence in the following way.

First, concatenate sequence AA infinitely many times. Then, for every positive integer ii, subtract ii from the ii-th number.

Pleased that the sequence had become longer, Sehun decided to pick several consecutive numbers from the resulting sequence and add them up. Since he likes large values, he wants to make this sum as large as possible.

For example, if AA is [9,1,7][9, 1, 7], the new sequence becomes [8,−1,4,5,−4,1,…][8, -1, 4, 5, -4, 1, \ldots], and its maximum contiguous sum is the sum of the 1st through 4th numbers, 8−1+4+5=168-1+4+5=16.

Input

The first line contains NN, the length of sequence AA. (1≤N≤500 0001 \le N \le 500\,000)

The second line contains the elements A1,A2,…,ANA_1, A_2, \ldots, A_N, separated by spaces. (∣Ai∣<231|A_i| < 2^{31})

Output

Print the answer to the problem.

Hint

Since the input is large, use fast input and output as described in the language guide on pages 2-4 of the problem booklet.

Examples2

  1. Example 1

    Input
    3
    9 1 7
    
    Expected output
    16
    
  2. Example 2

    Input
    2
    -12 -17
    
    Expected output
    0