博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
George and Job(动态规划)
阅读量:4134 次
发布时间:2019-05-25

本文共 1431 字,大约阅读时间需要 4 分钟。

The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn’t have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.

Given a sequence of n integers p1, p2, …, pn. You are to choose k pairs of integers:

[l1, r1], [l2, r2], …, [lk, rk] (1 ≤ l1 ≤ r1 < l2 ≤ r2 < … < lk ≤ rk ≤ n; ri - li + 1 = m), 

in such a way that the value of sum is maximal possible. Help George to cope with the task.

Input

The first line contains three integers n, m and k (1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers p1, p2, …, pn (0 ≤ pi ≤ 109).

Output

Print an integer in a single line — the maximum possible value of sum.

Examples

Input
5 2 1
1 2 3 4 5
Output
9
Input
7 1 3
2 10 7 18 5 33 0
Output
61
题意:从n个数中选出k段长度为m的连续数组,使得这k段加和最大。
所选取的段数可以是连续的,也可以不是连续的。我们使dp[i][j]代表着从前i个数字中选取j段的最大值。对于dp[i][j]来说,它可以是前i-1个数选取j段,也可以是前i-m段选取j-1段之后,再选取一段。
状态转移方程:dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+cnt);//cnt为从i-m到i的数字和。
代码如下:

#include
#define ll long longusing namespace std;const int maxx=5e3+100;ll a[maxx],sum[maxx];ll dp[maxx][maxx];int n,m,k;int main(){
scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i]; int x=m-1;ll cnt; for(int i=m;i<=n;i++) {
cnt=sum[i]-sum[i-x-1]; for(int j=1;j<=k;j++) {
dp[i][j]=max(dp[i-1][j],dp[i-m][j-1]+cnt); } } printf("%lld\n",dp[n][k]);}

努力加油a啊,(o)/~

转载地址:http://uytvi.baihongyu.com/

你可能感兴趣的文章