Infinite Sequence
Time limit2sMemory limit1024 MB
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 of 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 infinitely many times. Then, for every positive integer , subtract from the -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 is , the new sequence becomes , and its maximum contiguous sum is the sum of the 1st through 4th numbers, .
Input
The first line contains , the length of sequence . ()
The second line contains the elements , separated by spaces. ()
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.