The best vacation
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
In one country's calendar a year has months, month lasts days, and the days of a month are numbered from to . Years follow one another. You take a vacation of exactly consecutive days, and on day of a month your friend hugs you times.
The vacation may start on any day and continue past the end of the year into the next one. What is the maximum number of hugs you can get?
Input format
The first line contains and (, ). The second line contains integers ().
Output format
A single number — the maximum number of hugs.
Examples
Input
3 2 1 3 1
Output
5
Input
3 6 3 3 3
Output
12
Input
5 6 4 2 3 1 3
Output
15
Note
To handle the wrap-around, write the months twice. The best vacation always ends on the last day of some month: otherwise shifting it one day to the right gives at least as many hugs. Build two prefix sums — of days and of hugs (). For every ending month find the starting month with two pointers or binary search; only the last days of the first month are taken.
The answer reaches — a 64-bit type is needed. In the first example the days are 1 1 2 3 1, and the best choice is 2 3: the answer is .
Source: Codeforces, problem 1358D.
