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.

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 Input

5
1
3
1
5
2

Sample Output

43

Hint

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 <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; #define MAXN 2002
/*
区间DP,从后向前推,dp[i][j]表示首端元素为a[i],尾端为a[j]的情况
dp[i][j] = max(dp[i+1][j]+t*a[i],dp[i][j-1]+t*a[j])
*/
int a[MAXN],dp[MAXN][MAXN];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
dp[i][i] = n*a[i];//最后一个卖出元素是a[i]的情况
for(int l=;l<n;l++)
{
for(int i=;i+l<=n;i++)
{
int j = i+l;
dp[i][j] = max(dp[i+][j]+(n-l)*a[i],dp[i][j-]+(n-l)*a[j]);
}
}
printf("%d\n",dp[][n]);
return ;
}

O - Treats for the Cows 区间DP的更多相关文章

  1. POJ3186:Treats for the Cows(区间DP)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  2. POJ3086 Treats for the Cows(区间DP)

    题目链接  Treats for the Cows 直接区间DP就好了,用记忆化搜索是很方便的. #include <cstdio> #include <cstring> #i ...

  3. Treats for the Cows 区间DP POJ 3186

    题目来源:http://poj.org/problem?id=3186 (http://www.fjutacm.com/Problem.jsp?pid=1389) /** 题目意思: 约翰经常给产奶量 ...

  4. 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...

  5. poj3186 Treats for the Cows(区间)

    题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...

  6. POJ 3186Treats for the Cows(区间DP)

    题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...

  7. POJ 3186Treats for the Cows (区间DP)

    详见代码 #include <stdio.h> #include <algorithm> #include <string.h> using namespace s ...

  8. [luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)

    传送门 f[i][j][k] 表示 左右两段取到 i .... j 时,取 k 次的最优解 可以优化 k 其实等于 n - j + i 则 f[i][j] = max(f[i + 1][j] + a[ ...

  9. POJ 3186 Treats for the Cows ——(DP)

    第一眼感觉是贪心,,果断WA.然后又设计了一个两个方向的dp方法,虽然觉得有点不对,但是过了样例,交了一发,还是WA,不知道为什么不对= =,感觉是dp的挺有道理的,,代码如下(WA的): #incl ...

随机推荐

  1. [C++ STL] 各容器简单介绍

    什么是STL? 1.STL(Standard Template Library),即标准模板库,是一个高效的C++程序库. 2.包含了诸多常用的基本数据结构和基本算法.为广大C++程序员们提供了一个可 ...

  2. DP BestCoder Round #50 (div.2) 1003 The mook jong

    题目传送门 /* DP:这题赤裸裸的dp,dp[i][1/0]表示第i块板放木桩和不放木桩的方案数.状态转移方程: dp[i][1] = dp[i-3][1] + dp[i-3][0] + 1; dp ...

  3. PowerDesigner连接Oracle数据库(32位)反向生成物理数据模型

    PowerDesigner可以连接Oracle数据库进行反向生成物理数据模型,本文演示操作过程. 环境说明: 1)Windows8.1,Oracle11R2 32位. 2)PowerDesigner1 ...

  4. JS数组、数组和学生对象

    <html> <head> <meta charset="utf-8"> <title>JS</title> </ ...

  5. 一行python能做什么!

    主要收集了平常遇到的代码和网上的简单题目,然后尝试将代码压缩到一行,仅仅是娱乐一下~~~ −−−−−(1)−−−−−−−−−−−(1)−−−−−− 用一行python写出一个嵌套的字符串. def p ...

  6. [转]oracle 同义词 synonym

    本文转自:http://blog.csdn.net/generalfu/article/details/7906561 同义词定义 当一个用户想访问另外一个用户的表时, 需要在表前加用户名,总加表名不 ...

  7. ubuntu系统中查看本机cpu和内存信息的命令和用法

    https://zhidao.baidu.com/question/192966322.html 写出ubuntu linux系统中查看本机cpu和内存信息的命令和用法,以及如何解读这些命令 ubun ...

  8. CUDA 显存操作:CUDA支持的C++11

    CUDA9的编译器和语言改进 使用CUDA 9,nvcc编译器增加了对C ++ 14的支持,其中包括新功能 通用的lambda表达式,其中使用auto关键字代替参数类型; auto lambda = ...

  9. 场景分割:MIT Scene Parsing 与DilatedNet 扩展卷积网络

    MIT Scene Parsing Benchmark简介 Scene parsing is to segment and parse an image into different image re ...

  10. Clickhouse DDL&DML

    (1)添加列: alter table [db.]table_name add column column_name [type] [default_expr] [after name_after] ...