Cows snacks

题目描述 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 receives over a given period time.

The treats are interesting for many 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 a.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 milk yield of cows, and soon the cows have large sums of money I do not know how to spend. For this reason, John purchased a N (1≤N≤2000) copies delicious snacks to sell the cows. John sold a daily snack. John certainly hope after all these snacks sold to get the maximum benefit. These snacks have following interesting features:

• 1 in accordance with snacks. . N numbers, which are in a row in a long box. The box has openings at both ends, each of John

A day may be removed from either end of the outermost box.

• Similar to wine and delicious cheese, the longer these snacks store more delicious. Of course, so John can sell them a higher price.

• The initial value of each snack not necessarily the same. When John the purchase, the i-th initial value snacks for Vi (1≤Vi≤1000).

• If the i-th snack sold At a day after it was bought, its price is vi × a.

Vi is the box down from the top of the i-th initial value snacks. John told the initial value of all your snacks, and hope you can help him calculate, after these snacks all been sold, how much he can get the most money.

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 Output Input Sample # 1 copy 513 152 43 Description Copy Output # 1 / prompt Explanation of the sample:

Five treats. On the first day FJ can sell 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.

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,ans,v[2020],h[2020][2020];
int dfs(int s,int x,int y){
    if(y<x){
        return 0;
    }
    if(h[x][y]){
        return h[x][y];
    }
    h[x][y]=max(dfs(s+1,x+1,y)+s*v[x],dfs(s+1,x,y-1)+s*v[y]);
    return h[x][y];
}
int main () {
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&v[i]);
    }
    dfs(1,1,n);
    printf("%d",h[1][n]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11141044.html