D. The Best Vacation
题目大意: 算出连续x天最多的拥抱,一个月第i号就有i个拥抱
思路:双指针,扫描过去(每个月每个月的计算,最后超出的部分再一天一天算)
代码
:
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=4e5+5;
typedef long long ll;
int n,a[MAXN];
ll k,b[MAXN],s[MAXN],ans,sum;
ll calc(ll x){return x*(x+1)>>1;
}
int main(){scanf("%d%lld",&n,&k);for(int i=1; i<=n; i++)scanf("%d",a+i),a[n+i]=a[i];n<<=1;for(int i=1,j=1; i<=n; i++){b[i]=calc(a[i]);s[i]=s[i-1]+a[i];sum+=b[i];while(s[i]-s[j]>=k) sum-=b[j++];if(s[i]-s[j-1]-k>=0) ans=max(ans,sum-calc(s[i]-s[j-1]-k));//printf("j %d sum %lld\n",j,sum);}printf("%lld\n",ans);return 0;
}