The Best Vacation
Contest: Codeforces Round 645 (Div. 2)
Difficulty rating: 1900
Tags: binary search, brute force, greedy, implementation, two pointers

You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha.

You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $$$x$$$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $$$x$$$ consecutive (successive) days visiting Coronavirus-chan.

They use a very unusual calendar in Naha: there are $$$n$$$ months in a year, $$$i$$$-th month lasts exactly $$$d_i$$$ days. Days in the $$$i$$$-th month are numbered from $$$1$$$ to $$$d_i$$$. There are no leap years in Naha.

The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $$$j$$$ hugs if you visit Coronavirus-chan on the $$$j$$$-th day of the month.

You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan).

Please note that your trip should not necessarily begin and end in the same year.

Input format:
The first line of input contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of months in the year and the number of days you can spend with your friend.

The second line contains $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$, $$$d_i$$$ is the number of days in the $$$i$$$-th month ($$$1 \le d_i \le 10^6$$$).

It is guaranteed that $$$1 \le x \le d_1 + d_2 + \ldots + d_n$$$.

Output format:
Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $$$\{1,1,2,3,1\}$$$. Coronavirus-chan will hug you the most if you come on the third day of the year: $$$2+3=5$$$ hugs.

In the second test case, the numbers of the days are $$$\{1,2,3,1,2,3,1,2,3\}$$$. You will get the most hugs if you arrive on the third day of the year: $$$3+1+2+3+1+2=12$$$ hugs.

In the third test case, the numbers of the days are $$$\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$$$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $$$2+3+1+2+3+4=15$$$ times.

Example input:
3 2
1 3 1

Example output:
5

Example input:
3 6
3 3 3

Example output:
12

Editorial:
We will double the array of days and solve the problem when the day vacation starts and the day it ends is always in the same year. Then $$$a$$$ is an array of number of days in each of the months.
Consider array $$$B = [1, 2, ..., a_1] + [1, 2, ..., a_2] + ... [1, 2, ..., a_n] + [1, 2, ..., a_n]$$$. Our task is to find a subsection of length $$$k$$$ with the maximum sum in it. Further we will call this segment optimal.
Statement: We will find such an optimal segment that its end coincides with the end of some month.
Proof by contradiction: Pretend that's not the case. Consider the rightmost optimal segment. Let its last element be $$$x$$$, then the next one is $$$x+1$$$, otherwise $$$x$$$ coincides with $$$a_i$$$. Note that if we move this segment to the right, the sum must be reduced, which means that the first element of the segment $$$>x+1$$$. Then its left neighbor $$$>x$$$. It means that you can move the segment by $$$1$$$ to the left so that the sum increases. So, the chosen segment is not optimal. Contradiction. (see picture)

Solution: now we just need to go through all the possible ends of the segment, which are only $$$\mathcal{O}(n)$$$. Let's build two arrays of prefix sums:
$$$c_i = a_1 + a_2 + ... + a_i\\$$$ $$$d_i = \frac{a_1 (a_1 + z)}{2} + ... + \frac{a_i (a_i + 1)}{2}$$$
$$$c_i$$$ is responsible for the number of days before the $$$i$$$-th month, and $$$d_i$$$ is responsible for the sum of numbers of all days before the $$$i$$$-th month.
For each of the n ends, let's make a binsearch to find which month contains its left border ($$$k$$$ days less than the right one). You can use array $$$c_i$$$ to check whether the left border lies to the left/in the block/to the right, and use array $$$d_i$$$ to restore the answer.
The overall compexity is $$$\mathcal{O}(n\log{n})$$$.