Although this question looks like HDU 1024 Max sum plus, it seems easier than 1024.
Previously wa several times, because I started to write DP [22] [maxn] as DP [maxn] [22], orz
It seems that arrays out of bounds may not necessarily cause program crash or return an error.
DP [I] [J] indicates the maximum number of J numbers that constitute the first I segment.
State transition equation:
DP [I] [J] = max {DP [I] [J-1], DP [I-1] [J-len [I] + sum [J]-sum [J-len [I]}
Corresponding to not take the I number and take the J number and the adjacent total Len [I] Number respectively as the I segment plus the previous number constitute the maximum value of the I-1 segment
1 //#define LOCAL 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 1000 + 10; 8 int a[maxn], sum[maxn], len[22], dp[22][maxn]; 9 10 int main(void)11 {12 #ifdef LOCAL13 freopen("1244in.txt", "r", stdin);14 #endif15 16 int n, m;17 while(scanf("%d", &n) == 1 && n)18 {19 scanf("%d", &m);20 for(int i = 1; i <= m; ++i)21 scanf("%d", &len[i]);22 for(int i = 1; i <= n; ++i)23 scanf("%d", &a[i]);24 sum[0] = 0;25 for(int i = 1; i <= n; ++i)26 sum[i] = sum[i - 1] + a[i];27 memset(dp, 0, sizeof(dp));28 for(int i = 1; i <= m; ++i)29 {30 for(int j = 1; j < len[i]; ++j)31 dp[i][j] = dp[i][j-1];32 for(int j = len[i]; j <= n; ++j)33 dp[i][j] = max(dp[i][j-1], dp[i-1][j-len[i]] + sum[j] - sum[j-len[i]]);34 }35 printf("%d\n", dp[m][n]);36 }37 return 0;38 }
Code Jun
HDU 1244 Max sum plus