(二分查找+前缀和)Codeforces Round #645 (Div. 2) D - The Best Vacation

这题知道是末端一定是在月末这题就没啥了,可惜这题做的时候迷迷糊糊的,这都没想到,然后就s[i]表示前缀和(要扩大一倍),查一下s[i]-x在哪个地方就可以了,

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

Copy

3 2
1 3 1

Output

Copy

5

Input

Copy

3 6
3 3 3

Output

Copy

12

Input

Copy

5 6
4 2 3 1 3

Output

Copy

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.

#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#include<string>
#define maxn 1050000
ll d[maxn],s[maxn],p[maxn];
int main()
{
    ll n,x;
    scanf("%lld %lld",&n,&x);
    for(ll i=1;i<=n;i++)
    {
        scanf("%lld",&d[i]);
        s[i]=s[i-1]+d[i];
        p[i]=p[i-1]+(d[i]+1)*d[i]/2ll;
    }
    for(ll i=1;i<=n;i++)
    {
        d[n+i]=d[i];
        s[n+i]=s[n+i-1]+d[i];
        p[n+i]=p[n+i-1]+(d[i]+1)*d[i]/2ll;
        //printf("d[]")
    }
    //for(ll i=1;i<=2*n;i++) printf("d[%lld]=%lld s[%lld]=%lld p[%lld]=%lld\n",i,d[i],i,s[i],i,p[i]);
    ll mx=0;
    for(ll i=n+1;i<=2*n;i++)
    {
        ll w=0,a=0,j=lower_bound(s+1,s+1+2*n,s[i]-x)-s;
        w=p[i]-p[j];
        a=x-(s[i]-s[j]);
        a=d[j]-a;
        //printf("i=%lld w=%lld a=%lld j=%lld\n",i,w,a,j);
        w+=(d[j]+1)*d[j]/2ll-(a+1)*a/2ll;
        mx=max(mx,w);
    }
    printf("%lld\n",mx);
}

猜你喜欢

转载自blog.csdn.net/qq_43497140/article/details/106393682