/* Test Instructions: Select the number of 3 consecutive numbers The interval of K to make their and maximum analysis: dp[j][i]=max (Dp[j-k][i-1]+value[j],dp[j-1][i]); Dp[j][i]: The maximum and value[j of I continuous intervals values are selected from the J Number: the number in the J interval is a bit like the backpack, but the */ #include <cstdio> #include <cstring> #include <iostream>using namespace Std;int dp[50005][4]; int main () { int t; scanf ("%d", &t); while (t--) { int n; scanf ("%d", &n); int a[n+1],sum[n+1]; Memset (Dp,0,sizeof (DP)); memset (A,0,sizeof (a)); memset (sum , 0,sizeof (sum)); for (int i=1; i<=n; i++) { &NB Sp scanf ("%d", &a[i]); sum[i]=sum[i-1]+a[i];//prefix and, For the number of consecutive K } &NBSp int k=0; scanf ("%d", &k); int value[n+1]; &NB Sp memset (value,0,sizeof (value)); for (int i=1; i<=k; i++) &NB Sp value[i]=value[i-1]+a[i]; for (int i=k+1; i<=n; i++) value[i]=sum[i]-sum[i-k];//consecutive K numbers and Value[i] represent the interval length k for the interval for (int j=k; j<=n; j + +) for (int i=1; i<=3; i++) Dp[j][i]=max (dp[j-k][i-1]+value[j],dp[j-1][i]);//Select the I interval from the J number, It is equivalent to the number of previous (J-k) selected (I-1) The basis of the interval (value[j]), if not selected is equivalent to the number of (j-1) Select I range printf ("%d\n", Dp[n][3]); } return 0;}
A Mini locomotive (Dynamic planning)