Algorithm
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 n consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the i-th picture has beauty ai.
Vova wants to repost exactly x pictures in such a way that:
- each segment of the news feed of at least k 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=1 then Vova has to repost all the pictures in the news feed. If k=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.
The first line of the input contains three integers n,k and x (1≤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 n integers a1,a2,…,an (1≤ai≤109), where ai is the beauty of the i-th picture.
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.
5 2 3 5 1 3 10 1
18
6 1 5 10 30 30 70 10 10
-1
4 3 1 1 100 1 1
100
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 2e2 + 1;
int n, k, x, a[N];
long long dp[N][N][N];
long long rec(int idx, int prv, int rem) {
if(rem < 0 || prv >= k)
return -1e15;
if(idx == n)
return rem == 0 ? 0 : -1e15;
long long &ret = dp[idx][prv][rem];
if(ret != -1)
return ret;
ret = -1e15;
ret = max(ret, rec(idx + 1, prv + 1, rem));
ret = max(ret, rec(idx + 1, 0, rem - 1) + a[idx]);
return ret;
}
int main() {
scanf("%d %d %d", &n, &k, &x);
for(int i = 0; i < n; ++i)
scanf("%d", a + i);
memset(dp, -1, sizeof dp);
long long res = rec(0, 0, x);
if(res <= 0)
puts("-1");
else
printf("%lld\n", res);
return 0;
}
Copy The Code &
Try With Live Editor
Input
5 1 3 10 1
Output
Demonstration
Codeforces Solution-F1. Pictures with Kittens (easy version)-Solution in C, C++, Java, Python