Codeforces B. Array Walk

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;
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

henulmh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值