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.这样所有的情况就有了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=4e5+100;
int a[maxx];
ll sum[maxx],sum1[maxx];
int n;ll x;int main()
{scanf("%d%lld",&n,&x);for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=n+1;i<=2*n;i++) a[i]=a[i-n];sum[0]=sum1[0]=0;for(int i=1;i<=2*n;i++) sum[i]=sum[i-1]+a[i];for(int i=1;i<=2*n;i++) sum1[i]=sum1[i-1]+(1ll+(ll)a[i])*(ll)a[i]/2ll;int l=1,r=1;ll num=0;ll _max=0;while(r<=2*n){while(num<x&&r<=2*n) num+=a[r],r++;if(num<x) break;ll ans=0;ans+=(sum1[r-1]-sum1[l]);//这样我们就选取的最后一段长度为x的序列,但是这样有可能多,有可能少,分情况讨论。if(sum[r-1]-sum[l]>x) {ll xx=sum[r-1]-sum[l]-x;ans-=(1ll+xx)*xx/2ll;}else if(sum[r-1]-sum[l]<x){ll xx=x-sum[r-1]+sum[l];xx=(ll)a[l]-xx+1ll;ans+=(xx+(ll)a[l])*((ll)a[l]-xx+1ll)/2ll;}_max=max(_max,ans);num-=a[l];l++;}cout<<_max<<endl;return 0;
}

努力加油a啊,(o)/~

The Best Vacation CodeForces - 1358D(贪心+尺取)相关推荐

  1. CodeForces - 1198A MP3(尺取)

    题目链接:点击查看 题目大意:给出n个数字,表示不同的数据,现在我们需要对数据进行压缩,压缩的规则是: 现在给出一个I,表示内存大小为,n个数字中,相同的数据可以占用同一片内存,不同的数据必须占用不同 ...

  2. 2017百度之星初赛:B-1006. 小小粉丝度度熊(贪心+尺取)

    小小粉丝度度熊  Accepts: 1075  Submissions: 5191  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 327 ...

  3. CodeForces - 1358D The Best Vacation(前缀和+尺取)

    题目链接:点击查看 题目大意:给出 n 个数组成的数列,每个元素都可以展开为 1 , 2 , 3 .... a[ n ] ,现在将数列首尾相接,要求选取一段长度为 x 的连续数列,使得元素和最大 题目 ...

  4. Codeforces Round #736 (Div. 2) D. Integers Have Friends ST表gcd + 尺取

    传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,求一个最长的子序列[l,r][l,r][l,r]满足aimodm=ai+1modm=...=armodma_i\bmod m=a_{i+1 ...

  5. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  6. Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)

    排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...

  7. CodeForces - 1333C Eugene and an array(尺取)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数组 a,抛出 good 数组的定义: good 数组为数组 a 的一个子数组 good 数组的任意子数组之和均不为 0 (注意区分子数组和子数列的 ...

  8. 【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)

    题干: An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., a ...

  9. 【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)

    题干: The array a with n integers is given. Let's call the sequence of one or more consecutive element ...

最新文章

  1. 【Ghost Blog】如何给Ghost Blog添加背景音乐
  2. 串行和并行的区别_入门参考:从Go中的协程理解串行和并行
  3. c语言中的目标程序的正确含义,C语言程序设计练习题整理要点.doc
  4. C++11:右值引用和转移赋值
  5. python0表示剪刀_简化Python代码(石头、纸张、剪刀)
  6. 简单5步,释放Mac磁盘空间
  7. 01 安装STEP7软件和USB驱动
  8. 基于深度学习的实时激光雷达点云目标检测及ROS实现复现时出错解决方法汇总
  9. 电脑使用小常识(2):新手装软件指南,防止流氓软件
  10. 多线程-day-09CAS原理
  11. 程序语言 | 编程范式/泛型一览
  12. RK3229 android9.0 按刷机按键进入loader
  13. 光纤验收测试标准、参数及常用设备
  14. 天气选择页面html,CSS3 天气预报界面组件
  15. 腾讯汤道生:微信乘车码已全量上线昆明地铁
  16. nas文件服务器web接口,nas配置web服务器
  17. JS学习26:数组对象 之 数组转换为字符串
  18. 电表及配电监控系统的智能化发展
  19. linux 运行程序命令
  20. 一名软件工程师裸身跳楼身亡

热门文章

  1. 网站爬取工具_浅析阻碍网站内容被蜘蛛抓取的原因有哪些?
  2. java quot;1quot;==quot;1quot;_JAVA: 为什么要使用quot;抽象类quot;? 使用quot;抽象类quot;有什么好处?...
  3. linux p2p视频播放器,avplayer: 一个基于FFmpeg、libtorrent的P2P播放器实现.
  4. springfox源码_Spring boot整合Springfox在线生成restful的api doc
  5. 简单上手腾讯X5页面浏览
  6. 丹麦奥尔堡大学计算机系博士,丹麦奥尔堡大学招收计算机全奖PHD
  7. c语言二维数组省略号,LaTex 常用语法
  8. c 调用c语言写的dll文件路径,手把手教你用C/C++语言创建及调试动态库DLL程序
  9. iOS查看静态库命令
  10. android纵向列表菜单栏实现,RecyclerView实现常见的列表菜单