Problem - D - Codeforces
思路:一直在正向的考虑,这个题从反向考虑更加容易,首先如果k>=n的话,初始的一定都可以拿完,并且我们知道生长的蘑菇的总量是n*k个蘑菇,那么如果我们知道剩下了多少个蘑菇,那么就能够算出来拿了多少个生长的蘑菇,并且我们期望剩下的蘑菇和越少越好,那么怎么保证最小呢,就是我从最左边一直拿到最右边是最小的此时是1 2 3 4 ... n,那么我们就知道了最后的答案就是sum+n*k-n*(n+1)/2,并且一定存在一种方案使得最后是从左往右拿的,如果k==n+1,那么我们可以从2往左拿,再从1往右拿,同理其他情况也是;如果k<n,那么我们就是从某个开始一直往前拿最多能够取得的最大值,此时拿的生长的蘑菇的数量是k*(k-1)/2;
// Problem: D. The Enchanted Forest
// Contest: Codeforces - Codeforces Round 796 (Div. 2)
// URL: https://codeforces.com/contest/1688/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
#include<sstream>
#include<cassert>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}
int T,hackT;
int n,m,k;
int w[N];
ll sum[N];
struct Node{
int l,r;
ll sum;
ll add;
};
Node tr[N*4];
void pushup(int u) {
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}
void pushdown(int u) {
if(tr[u].add!=0) {
tr[u<<1].sum+=(ll)(tr[u<<1].r-tr[u<<1].l+1)*tr[u].add;
tr[u<<1|1].sum+=(ll)(tr[u<<1|1].r-tr[u<<1|1].l+1)*tr[u].add;
tr[u<<1].add+=tr[u].add;
tr[u<<1|1].add+=tr[u].add;
tr[u].add=0;
}
}
void build(int u,int l,int r) {
if(l==r) tr[u]={l,r,0,0};
else {
tr[u]={l,r,0,0};
int mid=l+r>>1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
}
}
void modify(int u,int l,int r,int c) {
if(tr[u].l>=l&&tr[u].r<=r) {
tr[u].sum+=(ll)(tr[u].r-tr[u].l+1)*c;
tr[u].add++;
}else {
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid) modify(u<<1,l,r,c);
if(r>mid) modify(u<<1|1,l,r,c);
pushup(u);
}
}
int query(int u,int l,int r) {
if(tr[u].l>=l&&tr[u].r<=r) return tr[u].sum;
else {
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
ll res=0;
if(l<=mid) res+=query(u<<1,l,r);
if(r>mid) res+=query(u<<1|1,l,r);
return res;
}
}
void solve() {
n=read(),k=read();
for(int i=1;i<=n;i++) w[i]=read();
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+w[i];
if(n==1) {
printf("%d\n",w[1]+k-1);
return ;
}
if(k>=n) {
ll res=sum[n]+(ll)n*k-(ll)n*(n+1)/2;
printf("%lld\n",res);
}else {
ll res=0;
for(int i=1;i+k-1<=n;i++) {
int l=i,r=i+k-1;
ll temp=sum[r]-sum[l-1]+(ll)k*(k-1)/2;
res=max(res,temp);
}
printf("%lld\n",res);
}
}
int main() {
// init();
// stin();
// ios::sync_with_stdio(false);
scanf("%d",&T);
// T=1;
while(T--) hackT++,solve();
return 0;
}