DP (l, r) = Max (DP (l + 1, R) + v_l * (N-r + L), DP (L, R-1) + V_r * (N-r + L))
Boundary: DP (i, i) = v[i] * n
--------------------------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = 5 +;int d[MAXN] [MAXN];int v[MAXN];int n;int dp (int l, int r) {int &ans = d[l [r];if (ans! =-1)return ans;ans = max (DP (l + 1, R) + (N-r + L) * v[L], DP (L, R-1) + (N-r + L) * v[R]);return ans;}int main () {//freopen ("test.in", "R", stdin);CLR (d,-1);cin >> N;Rep (i, N) {scanf ("%d", V + i);d[I [i] = n * v[i];}cout << dp (0, n-1) << "\ n";return 0;}
--------------------------------------------------------------------------------------------
1652: [Usaco2006 feb]treats for the cows time limit: 5 Sec Memory Limit: + MB
Submit: Solved: 199
[Submit] [Status] [Discuss] Description
FJ have purchased N (1 <= n <=) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats is interesting for many reasons: * The treats is numbered 1..N and stored sequentially in single file in a lo Ng box that's open at both ends. On any day, the FJ can retrieve one treat from either end of the stash. * Like fine wines and delicious cheeses, the treats improve with age and command greater prices. * The treats is not uniform:some is better and has higher intrinsic value. Treat I has value V (i) (1 <= v (i) <= 1000). * Cows pay more for treats that has aged longer:a cow would pay V (i) *a for a treat of age a. Given The values V (i) of each of the treats lined up and order of the index I in their box, what is the greatest value FJ C A receive for them if he orders their sale optimally? The first treat are sold on Day 1 and have age a=1. Each SUBsequent Day increases the age by 1.
John often gave special allowances to milk-producing cows, and soon the cows had a large amount of money they didn't know how to spend. For This, John bought N (1≤n≤2000) delicious snacks to sell to the cows. John sells a snack every day. Of course, John wants all these snacks to get the most out of the sale. These snacks have the following interesting features:
• Snacks in accordance with 1. n numbers, they are lined up in a long box. There were openings at both ends of the box, and John had everyThe day can be removed from either end of the box with the outermost one. • Similar to good wine and tasty cheeses, these snacks are stored for as long as possible. Of course, so John can sell them.for a higher price. • The initial value of each snack is not necessarily the same. When John was in stock, the initial value of the first snack was Vi (1≤vi≤1000). • If a snack is sold on the first day after being bought, it will be priced at Vixa. VI is the initial value of the part I snack from the top of the box. John told you the initial value of all the snacks andI hope you can help him figure out how much money he can get after all these snacks are sold.
Input
* Line 1: A single integer,
N * Lines 2..n+1:line i+1 contains the value of treat V (i)
Output
* Line 1:the maximum revenue FJ can achieve by selling the treats
Sample Input5
1
3
1
5
2
Five treats. On the first day FJ can sell either treat #1 (value 1) or
Treat #5 (value 2).
Sample Output43
OUTPUT DETAILS:
FJ sells the treats (values 1, 3, 1, 5, 2) in the following order
of Indices:1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.
HINT
Source
Silver
Bzoj 1652: [Usaco2006 feb]treats for the cows (DP)