D. The Enchanted Forest
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.
—Perfect Memento in Strict Sense
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.
我的想法是先判断k是否是大于n的,如果k大于等于n,则说明可以跑完所有所给的数字,如果k小于n,则说明不能跑完所有所给数字,然后先计算出所给的数字的前缀和,然后分情况讨论。
(1)若k>=n,说明可以跑完所有数字,因为每走一步,所有的数字是加一的,和你怎么走无关,所以可以单独出来考虑,然后既然所有数字都可以走到,就可以直接拿所有的数字和,然后我不管怎么走,最后几步都是加1到n个,然后k-n-1次都是拿取n个,所以答案就是1到n总和加上(k - n - 1)*n再加上所给数字的总和
(2)若k<n,说明不能跑完所有数字,就直接遍历前缀和数组,然后设置一个变量maxn,每次遍历更新前缀和最大值maxn,然后最后的答案是1到k之和加上maxn.
代码如下:
#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iomanip>
using namespace std;
#define ll long long
#define mod 1000000007
typedef pair<ll, ll> PII;
const int N = 1e6 + 10;
ll a[N];
ll sum[N],c[N];
void work()
{
ll n,k;
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
ll num = 0;
int i = 1;
if (k < n)
{
num += k * (k - 1) / 2;
}
else
{
num += (n + 1) * n / 2;
num += (k - 1 - n) * n;
}
ll maxn = 0;
if (k < n)
{
maxn = 0;
for (int i = 1, j = i + k - 1; j <= n; j++, i++)
{
maxn = max(sum[j] - sum[i - 1], maxn);
}
}
else
{
maxn = sum[n];
}
maxn += num;
cout << maxn << "\n";
}
int main()
{
ll t = 1;
cin >> t;
while (t--)
{
work();
}
return 0;
}