POJ 3186 Treats for the Cows
The first thought was to use a flag to record the time for retrieving the number of I, but later I found that this method is not good, there are too many possibilities, and there is no way to push it.
Then let's take a look at the ideas of the report, saying that the dp is used, and the status is set up. However, due to the influence of the inherent thinking, I always want to push it along.
Finally, I couldn't think of it. After reading the code, I found that I had to push it backwards. I started to push it from the end, so the formula came out.
To ensure that the values used in each layer of the loop have been calculated, enumerate by Interval Length.
To commemorate the first dp Interval
/* Dp [I] [j]: When I to j items are left, remove the maximum value that can be obtained for all the remaining items dp [I] [j] = max (dp [I + 1] [j] + v [I] * (n-(j -I + 1) + 1), dp [I] [J-1] + v [j] * (n-(j-I + 1) + 1); */# include
# Include
Using namespace std; int n, v [2005], dp [2005] [2005]; void init () {// memset (dp,-1, sizeof (dp )); for (int I = 1; I <= n; I ++) dp [I] [I] = v [I] * n;} int main () {# ifndef ONLINE_JUDGEfreopen ("in.txt", "r", stdin); # endifscanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d", & v [I]); int tmp; init (); int j; for (int l = 2; l <= n; l ++) {// reverse push, starts from the beginning of the last outbound queue, enumerative by Interval Length for (int I = 1; I <= n; I ++) {j = I + L-1; dp [I] [j] = max (dp [I + 1] [j] + v [I] * (L-1 )), dp [I] [J-1] + v [j] * (L-1);} printf ("% d \ n ", dp [1] [n]);}