Codeforces Round #645 (Div. 2) D. The Best Vacation

Codeforces Round #645 (Div. 2) D. The Best Vacation

題目鏈接
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≤2e5) — 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≤1e6).

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

典型的前綴和加二分~
題意就是在這些天裏面找一段連續的天數 xx,使得總和最大。因爲每年的天數是循環的,所以答案肯定就出現在兩年內~
pre1pre1 記錄數組 dd 的前綴和,對每一個 dd,總擁抱數即爲 d(d+1)/2d*(d+1)/2,我們再用 pre2pre2 記錄持續天數的前綴和,然後遍歷計算即可~
對每一個大於 xx 的前綴和 pre1[i+1]pre1[i+1],我們二分查找出大於 prexpre-x 的前綴和的位置 pospos,那麼現有的總擁抱數即爲 pre2[i+1]pre2[pos]pre2[i+1]-pre2[pos],總天數 dayday 即爲pre1[i+1]pre1[pos]pre1[i+1]-pre1[pos],但此時 dd 可能小於 xx,所以我們要補上少的這部分,通過計算不難發現這部分即爲 d[pos1](d[pos1]+1)/2(d[pos1]res)(d[pos1]res+1)/2d[pos-1]*(d[pos-1]+1)/2-(d[pos-1]-res)*(d[pos-1]-res+1)/2,每次更新一下答案即可,AC代碼如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
main(){
   ll n,x,ans=0;
   cin>>n>>x;
   vector<ll>d(2*n),pre1={0},pre2={0};
   for(ll i=0;i<n;i++) cin>>d[i],d[n+i]=d[i];
   for(ll i=0;i<2*n;i++) pre1.push_back(pre1.back()+d[i]),pre2.push_back(pre2.back()+d[i]*(d[i]+1)/2);
   for(ll i=0;i<2*n;i++){
        if(pre1[i+1]>=x){
            ll pos=upper_bound(pre1.begin(),pre1.end(),pre1[i+1]-x)-pre1.begin();
            ll cnt=pre2[i+1]-pre2[pos];
            ll day=pre1[i+1]-pre1[pos];
            ll res=x-day;
            cnt+=d[pos-1]*(d[pos-1]+1)/2-(d[pos-1]-res)*(d[pos-1]-res+1)/2;
            ans=max(ans,cnt);
        }
    }
    cout<<ans;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章