The Best Vacation CodeForces - 1358D(贪心+尺取)
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 di days. Days in the i-th month are numbered from 1 to di. 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
The first line of input contains two integers n and x (1≤n≤2?105) — the number of months in the year and the number of days you can spend with your friend.
The second line contains n integers d1,d2,…,dn, di is the number of days in the i-th month (1≤di≤106).
It is guaranteed that 1≤x≤d1+d2+…+dn.
Output
Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.
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
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.
思路:挺明顯的一道尺取的題目,但是呢,又不能全部展開,因為那樣必定超時。我們可以發現,如果當前我們選定的范圍值[l,r](第l個數到第r個數),那么怎么選擇是最優的呢?就是選擇這個區間里最后一段長度為x的序列,這樣是最優的(可以自行證明)。因為類似于一個環形,那么我們可以在數組后面再增加一個數組,這樣就可以線性處理了,例如數組為 a,b,c,d,我們將之變為a,b,c,d,a,b,c,d.這樣所有的情況就有了。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的The Best Vacation CodeForces - 1358D(贪心+尺取)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线即时展现 Html、JS、CSS 编
- 下一篇: 什么是下折?