P2858 [usaco 06feb] dairy snacks Treats for the Cows, p2858usaco 06feb
Description
FJ has purchased N (1 <= N <= 2000) 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 has es over a given period time.
The treats are interesting for each reasons: The treats are numbered 1 .. N and stored sequentially in single file in a long box that is open at both ends. on any day, FJ can retrieve one treat from either end of his stash of treats. like fine wines and delicious cheeses, the treats improve with age and command greater prices. the treats are not uniform: some are better and have higher intrinsic value. Treat I has value v (I) (1 <= v (I) <= 1000 ). cows pay more for treats that have aged longer: a cow will pay v (I) * a for a treat of age. given the values v (I) of each of the treats lined up in order of the index I in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?
The first treat is sold on day 1 and has age a = 1. Each subsequent day increases the age by 1.
John often made special allowances for high-paying cows, so soon the cows had a lot of money they didn't know how to spend. john purchased N (1 ≤ N ≤ 2000) delicious snacks to sell to the cows. john sold a snack every day. of course, John hopes that all these snacks will get the biggest benefit after they are sold. these snacks have the following interesting features:
• Snacks are numbered according to 1. N. They are arranged in a column in a long box. There are openings at both ends of the box, John
Days can be taken from any end of the box the outermost one.
• Similar to wine and delicious cheese, the longer these snacks are stored, the better. Of course, John can sell them for a higher price.
• The initial value of each snack is not necessarily the same. When John purchases the item, the initial value of the I-part snack is Vi (1 ≤ Vi ≤ 1000 ).
• If the I-th snack is sold on the-th day after the purchase, its price is vi ×.
Vi is the initial value of the I-th snack from the top of the box. john tells you the initial value of all snacks and hopes that you can help him calculate the maximum amount of money he can get after all these snacks are sold.
Input/Output Format
Input Format:
Line 1: A single integer, N
Lines 2. N + 1: Line I + 1 contains the value of treat v (I)
Output Format:
Line 1: The maximum revenue FJ can achieve by selling the treats
Input and Output sample input sample #1:
513152
Output sample #1:
43
Description
Explanation of the sample:
Five treats. On the first day FJ can have either treat #1 (value 1) or treat #5 (value 2 ).
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.
Dp [I] [j] indicates that I is obtained on the left and the maximum value of j is obtained on the right.
The k-th request to be retrieved is n-(j-I)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 using namespace std; 7 const int MAXN=6001; 8 void read(int &n) 9 {10 char c='+';int x=0;bool flag=0;11 while(c<'0'||c>'9')12 {c=getchar();if(c=='-')flag=1;}13 while(c>='0'&&c<='9')14 {x=x*10+(c-48);c=getchar();}15 flag==1?n=-x:n=x;16 }17 int n;18 int dp[2001][2001];19 int a[2001];20 int main()21 {22 read(n);23 for(int i=1;i<=n;i++)24 read(a[i]);25 for(int i=n;i>=1;i--)26 for(int j=i;j<=n;j++)27 dp[i][j]=max(dp[i+1][j]+a[i]*(n-j+i),dp[i][j-1]+a[j]*(n-j+i));28 printf("%d",dp[1][n]);29 return 0;30 }