B. Teamwork(dp)

题目链接

B. Teamwork

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

For his favorite holiday, Farmer John wants to send presents to his friends. Since he isn't very good at wrapping presents, he wants to enlist the help of his cows. As you might expect, cows are not much better at wrapping presents themselves, a lesson Farmer John is about to learn the hard way.

扫描二维码关注公众号,回复: 5283750 查看本文章

Farmer John's NN cows (1≤N≤1041≤N≤104) are all standing in a row, conveniently numbered 1…N1…N in order. Cow ii has skill level sisi at wrapping presents. These skill levels might vary quite a bit, so FJ decides to combine his cows into teams. A team can consist of any consecutive set of up to KK cows (1≤K≤1031≤K≤103), and no cow can be part of more than one team. Since cows learn from each-other, the skill level of each cow on a team can be replaced by the skill level of the most-skilled cow on that team.

Please help FJ determine the highest possible sum of skill levels he can achieve by optimally forming teams.

Input

The first line of input contains NN and KK. The next NN lines contain the skill levels of the NN cows in the order in which they are standing. Each skill level is a positive integer at most 105105.

Output

Please print the highest possible sum of skill levels FJ can achieve by grouping appropriate consecutive sets of cows into teams.

Example

input

Copy

7 3
1
15
7
9
2
5
10

output

Copy

84

Note

In this example, the optimal solution is to group the first three cows and the last three cows, leaving the middle cow on a team by itself (remember that it is fine to have teams of size less than KK). This effectively boosts the skill levels of the 7 cows to 15, 15, 15, 9, 10, 10, 10, which sums to 84.

题目大意:

给出n头牛,最多k头牛组成一组。每头牛有一个值,组成一组后,组中每头牛的值都会变成组中值最大。求分组后每头牛的值得和。

题目分析:

没想到用dp来做。dp的练习还要加强。

用dp[i]表示前i-1头牛的最大权值和,向后扫k个更新最大值。

dp[i+j]=max(dp[i+j],dp[i]+maxn*j);

maxn=max(maxn,a[i+j]);

组中元素每增加一个,判断加入后值是否更大,否则分在下一组。

代码:

#include<bits/stdc++.h>
using namespace std;
int a[100005],dp[100005];
int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++){
		int maxn=a[i];
		for(int j=1;j<=k;j++){
			dp[i+j]=max(dp[i+j],dp[i]+j*maxn);
			maxn=max(maxn,a[i+j]);
		}
	}
	cout<<dp[n+1];
} 

猜你喜欢

转载自blog.csdn.net/qq_43490894/article/details/87531481