Marisa comes to pick mushrooms in the Enchanted Forest.
The Enchanted forest can be represented by nn points on the XX-axis numbered 11 through nn. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by a1,a2,…,ana1,a2,…,an.
Marisa can start out at any point in the forest on minute 00. Each minute, the followings happen in order:
- She moves from point xx to yy (|x−y|≤1|x−y|≤1, possibly y=xy=x).
- She collects all mushrooms on point yy.
- A new mushroom appears on each point in the forest.
Note that she cannot collect mushrooms on minute 00.
Now, Marisa wants to know the maximum number of mushrooms she can pick after kk minutes.
Input
Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers nn, kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1091≤k≤109) — the number of positions with mushrooms and the time Marisa has, respectively.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial number of mushrooms on point 1,2,…,n1,2,…,n.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, print the maximum number of mushrooms Marisa can pick after kk minutes.
Example
input
Copy
4 5 2 5 6 1 2 3 5 7 5 6 1 2 3 1 2 999999 5 70000 1000000000 1000000000 1000000000 1000000000 1000000000
output
Copy
12 37 1000000 5000349985
Note
Test case 1:
Marisa can start at x=2x=2. In the first minute, she moves to x=1x=1 and collect 55 mushrooms. The number of mushrooms will be [1,7,2,3,4][1,7,2,3,4]. In the second minute, she moves to x=2x=2 and collects 77 mushrooms. The numbers of mushrooms will be [2,1,3,4,5][2,1,3,4,5]. After 22 minutes, Marisa collects 1212 mushrooms.
It can be shown that it is impossible to collect more than 1212 mushrooms.
Test case 2:
This is one of her possible moving path:
2→3→2→1→2→3→4→52→3→2→1→2→3→4→5
It can be shown that it is impossible to collect more than 3737 mushrooms.
思路:题目大致是说让一个人去割菜,一割会全割完。但是这里需要注意的是当割过后才会长1长度,不是割前就长1,这一点非常重要,会影响之后的等差数列求和的首项大小和末项大小。现在开始想如何收割呢?首先是与n和k的大小关系有关:
当k小于n时:这个好想,简单,我们只要选出连续的几个值之和最大即可,增长不会产生影响,因为时间太少选哪几个增长都是一样的,唯一有区别的是选本来就多的,这样就能割的更多。
当k大于n时:这个问题难,但是首先可以知道的是我们要全收割,因为本来有的话一定比不收割它值为0要大,因为有的话最小是1,也会比0大。然后难点就来了我们如何处理增长值呢?因为增长是全体的,我们可以考虑真正的增长速率。如果我们让时间刚好到时刚好遍历收割完一遍,那么增长速率就是n,即全部。如果当时间到时我们的位置在除端点外的任意位置的话,增长速率的总体就会减少,因为有几个在增长时没有算上,可以拿样例2来举例子,假设从时间刚开始时就从左端出发,跑到最右端后继续拐回来,走两步后停下来,时间到了,那么前的数增长的就没算上,(这里要明白不管最后到端点或者任意位置,总会必定舍弃有n*(n+1)/2个菜,因为跑过后还在增长。那么全部菜的增长就是n*k,可以用 总共的 - 舍弃的 = 得到的)假设我们在端点一直等到剩n的时间才走,那么既可以有全部的菜,又可以得到增长的,增长的可以用等差数列求和公式:n*(n+1)/2,这里说明一下求和公式的答案一定是整数,因为n与n+1必定有一个是偶数能整除2。这里注意得到的数是(从0开始的(是增长))k-n开始的,k-1结束的求和即可。
连续数组的最大值求法:先求前缀和,然后用前缀和数组sum减去前x个即可
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];//求前缀和
}
for(int i=k;i<=n;i++)
{
maxx=max(maxx,sum[i]-sum[i-k]);//得到最大连续子段
}
完整代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
const int N=2e5+10;
int a[N];
int sum[N];
void solve()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
if(k<n)
{
int maxx=0;
for(int i=k;i<=n;i++)
{
maxx=max(maxx,sum[i]-sum[i-k]);
}
//int other=k*k-k*(k+1)/2;//割完随时长,不是割前就长
int other=k*(k-1)/2;
cout<<maxx+other<<endl;
}
else
{
int maxx=sum[n];
//int other=n*k-(n+1)*n/2;
int other=n*(k-n+k-1)/2;
cout<<maxx+other<<endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}