The number of consecutive small sequences with a length of N is divided into K m-long continuous small sequences.
Solution: Apparently DP.
Definition: DP [I] [J] divides the first I elements into j m ends, and I is the largest sum at the end of J.
So there are: DP [I] [J] = max (DP [I-1] [J], DP [I-m] [J-1] + sum [I]-sum [I-m])
5000*5000 space ..
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define lll __int64using namespace std;lll dp[5002][5001];lll a[5002],sum[5003];int main(){ int n,m,k; int i,j; while(scanf("%d%d%d",&n,&m,&k)!=EOF) { sum[0] = 0; for(i=1;i<=n;i++) { scanf("%I64d",&a[i]); sum[i] = sum[i-1]+a[i]; } for(i=0;i<=n;i++) for(j=0;j<=k;j++) dp[i][j] = 0; for(i=m;i<=n;i++) { dp[i][0] = max(dp[i][0],dp[i-1][0]); for(j=1;j<=k;j++) { dp[i][j] = max(dp[i][j],dp[i-1][j]); dp[i][j] = max(dp[i][j],dp[i-m][j-1]+sum[i]-sum[i-m]); } } cout<<dp[n][k]<<endl; } return 0;}
View code
Codeforces round #267 div2 C George and job -- DP