Codeforces Pictures with Kittens (easy version) DP

F1. Pictures with Kittens (easy version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

Copy

5 2 3
5 1 3 10 1

output

Copy

18

input

Copy

6 1 5
10 30 30 70 10 10

output

Copy

-1

input

Copy

4 3 1
1 100 1 1

output

Copy

100

题目链接:http://codeforces.com/problemset/problem/1077/F1

题目大意:满足任意连续的k个数至少有一个选中,问选中x个的最大值,如果不存在输出-1,否则输出最大值

#include 
using namespace std ;
typedef long long ll ;
typedef pair < ll, ll > P ;
const ll Maxn = 5e3 + 10 ;
const ll INF = 0x3f3f3f3f ;

ll dp[Maxn][Maxn] ;
ll a[Maxn] ;
ll n, k, x ;
priority_queue < P > que ;

int main (){
    cin >> n >> k >> x ;
    for (ll i = 1; i <= n; i++) cin >> a[i] ;
    n++ ;
    x++ ;
    for (ll i = 0; i <= x; i++){
        for (ll j = 0 ;j <= n; j++){
            dp[i][j] = -INF ;
        }
    }
    dp[0][0] = 0 ;
    for (ll i = 1; i <= x; i++){
        for (ll j = i; j <= n; j++){
//            cout << j - 1 + k << endl ;
            que.push(make_pair(dp[i - 1][j - 1], j - 1 + k)) ;
//            cout << dp[i - 1][j - 1] << " " << j - 1 + k << endl ;
            while (que.top().second < j) que.pop() ;
            if (que.top().first < 0) break ;
            dp[i][j] = que.top().first + a[j] ;
        }
        while (!que.empty()) que.pop() ;
    }
    dp[x][n] = max(-1ll, dp[x][n]) ;
    cout << dp[x][n] << endl ;
    return 0 ;
}

F2. Pictures with Kittens (hard version)

链接:https://codeforces.com/problemset/problem/1077/F2

对于增强版也是可以实用的。

你可能感兴趣的:(DP)