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

典型的前缀和加二分~
题意就是在这些天里面找一段连续的天数 xxx,使得总和最大。因为每年的天数是循环的,所以答案肯定就出现在两年内~
用 pre1pre1pre1 记录数组 ddd 的前缀和,对每一个 ddd,总拥抱数即为 d∗(d+1)/2d*(d+1)/2d∗(d+1)/2,我们再用 pre2pre2pre2 记录持续天数的前缀和,然后遍历计算即可~
对每一个大于 xxx 的前缀和 pre1[i+1]pre1[i+1]pre1[i+1],我们二分查找出大于 pre−xpre-xpre−x 的前缀和的位置 pospospos,那么现有的总拥抱数即为 pre2[i+1]−pre2[pos]pre2[i+1]-pre2[pos]pre2[i+1]−pre2[pos],总天数 daydayday 即为pre1[i+1]−pre1[pos]pre1[i+1]-pre1[pos]pre1[i+1]−pre1[pos],但此时 ddd 可能小于 xxx,所以我们要补上少的这部分,通过计算不难发现这部分即为 d[pos−1]∗(d[pos−1]+1)/2−(d[pos−1]−res)∗(d[pos−1]−res+1)/2d[pos-1]*(d[pos-1]+1)/2-(d[pos-1]-res)*(d[pos-1]-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;
}

Codeforces Round #645 (Div. 2) D. The Best Vacation相关推荐

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

    题目链接:https://codeforces.com/contest/1358/problem/D 思路:双指针+前缀和 前缀和主要处理了两组数据:sum[]是某月到某月的天数,ans[] 代表某月 ...

  2. Codeforces Round #645 (Div. 2) D - The Best Vacation 题解(二分+思维)

    题目链接 题目大意 一年有n个月,每个月有d[i]天,让你找出连续x天,使其日期的总和最大,可以跨年 题目思路 这里要发现一个性质:即连续的x天一定满足最后一天在月份的结尾,结论是显然的. 然后用两个 ...

  3. codeforces Round #645 (Div. 2)D题解

    Codeforces Round #645 (Div. 2)--D题解 作为一名菜鸡,理所当然得没有A出来,这道题数据放小就一水题了,可惜数据这块卡的死死的. 本题最重要的一点就是你要推出来一个结论: ...

  4. Codeforces Round #645 (Div. 2)(AB)

    Park Lighting CodeForces - 1358A 思路:水题不解释. 代码如下: #include<bits/stdc++.h> #define ll long long ...

  5. Codeforces Round #645 (Div. 2) / contest 1358

    目录 A Park Lighting B Maria Breaks the Self-isolation C Celex Update D The Best Vacation E Are You Fi ...

  6. 双指针--Codeforces Round #645 (Div. 2) d题

    D. The Best Vacation 题目大意: 算出连续x天最多的拥抱,一个月第i号就有i个拥抱 思路:双指针,扫描过去(每个月每个月的计算,最后超出的部分再一天一天算) 代码 : #inclu ...

  7. 思维--找规律--Codeforces Round #645 (Div. 2) c题

    C. Celex Update 题目大意:给出两点的坐标,找出不同的路径的总数(路径数字总和不同) 思路:根据观察向下走比向右走的增加幅度加1,所以在第i步 向下 对sum的影响是 n-i+1 所以最 ...

  8. Codeforces Round #645 (Div. 2) E - Are You Fired? 题解(思维)

    题目链接 题目大意 给你一个长为n的数组前(n+1)/2个数为a[i],后面的数为x,让你求出一个长度k使其所有连续为k的数组之和都大于0 题目思路 首先要找性质,如果k满足,那么显然k*2也满足那么 ...

  9. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

最新文章

  1. 决策树模型与学习《一》
  2. 区块链论文研读12:谨慎日志合约 Discreet Log Contracts,详细 清晰 通俗易懂
  3. __proto__(隐式原型)与prototype(显式原型)
  4. Python简明教程
  5. 2009年浙江大学计算机及软件工程研究生机试真题
  6. 转: ant condition使用
  7. Oracle安装 - shmmax和shmall设置
  8. C++判断字符是字母或数字
  9. K8S专题-基础组件的部署1
  10. Storm处理流程, 基本参数配置
  11. VirtualBox安装MAC虚拟机,屏幕分辨率小,扩大的办法
  12. 2018年华北五省计算机应用大赛参赛作品--战拖儿app
  13. 多个斗鱼号怎么批量管理?
  14. 尚硅谷宋红康java基础学习笔记
  15. SQL注入实战 绕WTS-WAF
  16. outlook 2019 mac中如何设置邮件签名
  17. 网站变成灰色(置灰)
  18. 胆结石饮食有什么禁忌?
  19. 数据类型(int、short、long、long long、unsigned、char、float、double)
  20. ue4-摄像机动画Matinee(多图慎入)

热门文章

  1. springboot文献综述
  2. 【SP】SubstancePainter安装相关问题
  3. 朗伯辐射强度模型MATLAB,朗伯体辐射出射度与辐亮度的关系.PPT
  4. Mac 电脑python 升级3.7版本
  5. 网易博客fengqing888搬家至CSDN啦
  6. QT如何实现后台运行(即最小化到右下角托盘)
  7. Android应用开发 00:Jetpack Compose学习 生日贺卡 图片 Compose象限 名片
  8. osi七层模型_每天学一点教你巧记OSI七层网络模型
  9. Sublime Text 2搭建Go开发环境(Windows)
  10. Mar. 6, 15:00-17:00, 1493, Strichartz analysis for Schrodinger and wave equation I by Chong Chen