Title: C. George and jobtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
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 sequenceNIntegersP1 ,?P2 ,?...,?PN. You are to chooseKPairs of integers:
[
L1 ,?
R1],? [
L2 ,?
R2],?...,? [
L
K,?
R
K] (1? ≤?
L1? ≤?
R1? <?
L2? ≤?
R2? <?...? <?
L
K? ≤?
R
K? ≤?
N;
R
I? -?
L
I? +? 1? =?
M),?
In such a way that the value of sum is maximal possible. Help George to interact with the task.
Input
The first line contains three IntegersN,MAndK(1? ≤? (M? ×?K)? ≤?N? ≤? (5000). The second line containsNIntegersP1 ,?P2 ,?...,?PN(0? ≤?PI? ≤? 109 ).
Output
Print an integer in a single line-the maximum possible value of sum.
Sample test (s) Input
5 2 11 2 3 4 5
Output
9
Input
7 1 32 10 7 18 5 33 0
Output
61
Question Analysis: DP. DP [I] [J] before saving I has the maximum value of group J.
State transition equation:
if(i-m>=0)dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i-m][j-1]+b[i]-b[i-m])) ;
else
<span style="white-space:pre"></span>dp[i][j] = max(dp[i][j],dp[i-1][j]) ;
B [I] saves the sum of I items A [I.
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;long long dp[5005][5005] ;long long a[65555] ;long long b[65555] ;int main(){int n,m,k ;cin>>n>>m>>k;int i,j;for(i=1;i<=n;i++){cin>>a[i];b[i] = b[i-1] + a[i] ;}for(i=1;i<=n;i++){for(j=1;j<=k;j++){if(i-m>=0)dp[i][j] = max(dp[i][j],max(dp[i-1][j],dp[i-m][j-1]+b[i]-b[i-m])) ;else dp[i][j] = max(dp[i][j],dp[i-1][j]) ;}}cout<<dp[n][k]<<endl;return 0 ;}
Codeforces round #267 (Div. 2) c