You are given an array a1,a2,…,an, consisting of n positive integers.
Initially you are standing at index 1 and have a score equal to a1. You can perform two kinds of moves:
move right — go from your current index x to x+1 and add ax+1 to your score. This move can only be performed if x<n.
move left — go from your current index x to x−1 and add ax−1 to your score. This move can only be performed if x>1. Also, you can’t perform two or more moves to the left in a row.
You want to perform exactly k moves. Also, there should be no more than z moves to the left among them.
What is the maximum score you can achieve?
Input
The first line contains a single integer t (1≤t≤104) — the number of testcases.
The first line of each testcase contains three integers n,k and z (2≤n≤105, 1≤k≤n−1, 0≤z≤min(5,k)) — the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform.
The second line of each testcase contains n integers a1,a2,…,an (1≤ai≤104) — the given array.
The sum of n over all testcases does not exceed 3⋅105.
Output
Print t integers — for each testcase output the maximum score you can achieve if you make exactly k moves in total, no more than z of them are to the left and there are no two or more moves to the left in a row.
Example
input
4
5 4 0
1 5 4 3 2
5 4 1
1 5 4 3 2
5 4 4
10 20 30 40 50
10 7 3
4 6 8 2 9 9 7 4 10 9
output
15
19
150
56
Note
In the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score a1+a2+a3+a4+a5.
In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be a1+a2+a3+a2+a3.
In the third example you can move four times to the left but it’s not optimal anyway, you can just move four times to the right and obtain the score a1+a2+a3+a4+a5.
题意:
给一个数列 ,初始价值为 a[1],每次你可以选择向右走或者向左走,走完获得当前位置的数值,最多向左走 z 次,并且不能连续两次向左走,问走 k 步获得的最大价值。
思路:从1~k sum+=a[i]来记录和
实现找到相邻最大的两个和
求最多反复在最大两个值之间来回几次
(是我想不到的思路了。。。)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t,n,k,z,u,maxx,ans;
int a[100005];
string s;
cin>>t;
while(t--)
{
maxx=-1;
ans=-1;
cin>>n>>k>>z;
for(int i=1;i<=n;i++)
cin>>a[i];
int sum=0;
k++; //需要执行k++,便于后续的处理
for(int i=1;i<=k;i++)
{
sum+=a[i]; //记录在找到最大的两个连续的数之前走过的值
if(i<n)
{
maxx=max(maxx,a[i]+a[i+1]); //每次循环找到最大的 一组a[i] + a[i+1]
}
ans=max(ans,min((k-i)/2,z)*maxx+sum); //min((k-i)/2,z) 表示这两个数能反复走几次,然后乘maxx 之后 再加上之前的sum
}
cout<<ans<<endl;
}
}