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:
in such a way that the value of sum is maximal possible. Help George to cope with the task.
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).
Print an integer in a single line — the maximum possible value of sum.
5 2 1 1 2 3 4 5
9
7 1 3 2 10 7 18 5 33 0
61
题目大意:一个长度为n的序列选k组,每组长度为m,问最优的选法
ac代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#define LL long long
using namespace std;
LL sum[5050];
LL dp[5050][5050];
int n,m,k;
LL DP()
{
int i,j;
memset(dp,0,sizeof(dp));
for(i=m;i<=n;i++)
{
for(j=1;j<=i&&j<=k;j++)
{
dp[i][j]=max(dp[i-m][j-1]+(sum[i]-sum[i-m]),dp[i-1][j]);
}
}
return dp[n][k];
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
int i;
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
{
LL x;
scanf("%lld",&x);
sum[i]=sum[i-1]+x;
}
printf("%lld\n",DP());
}
}