CodeForces - 1077F1 (dp)

The only difference between easy and hard versions is the constraints.

Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of nn consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the ii-th picture has beauty aiai.

Vova wants to repost exactly xx pictures in such a way that:

  • each segment of the news feed of at least kk consecutive pictures has at least one picture reposted by Vova;
  • the sum of beauty values of reposted pictures is maximum possible.

For example, if k=1k=1 then Vova has to repost all the pictures in the news feed. If k=2k=2 then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.

Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.

Input

The first line of the input contains three integers n,kn,k and xx (1≤k,x≤n≤2001≤k,x≤n≤200) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the beauty of the ii-th picture.

Output

Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.

Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.

这题推荐去看该大佬的博客。。。下面是链接

https://www.cnblogs.com/pkgunboat/p/9974886.html

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
#define LL long long
const int MAX = 250;

LL dp[MAX][MAX];
LL a[MAX];
int main(){
	int n, m, x;
	scanf("%d%d%d", &n, &m, &x);
	for(int i = 1; i <= n; i++){
		scanf("%lld", &a[i]);
	}

	int mi = 1;
	for(int i = 1; i <= n; i++){
		for(int j = mi; j <= min(x, i); j++){ // j不能从每次1开始,再有些i的情况下j不能取1
			for(int k = max(i - m, 0); k < i; k++){
				dp[i][j] = max(dp[k][j - 1] + a[i], dp[i][j]);
			}
		}
		if(i % m == 0){
			mi++;
		}
	}

	LL ans = 0;
	for(int i = n - m + 1; i <= n; i++){ // 一定要从n - m + 1开始遍历,因为n - m + 1 到 n必须有个点被选
		ans = max(ans, dp[i][x]);
	}
	if(n / m > x){
		printf("-1\n");
	} else{
		printf("%lld\n", ans);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43737952/article/details/89076794