首页 > 代码库 > poj 3186 Treats for the Cows dp

poj 3186 Treats for the Cows dp

 

#include <cstdio>#include <algorithm>using namespace std;#define maxn 2100int dp[maxn][maxn];int val[maxn];int n;int main(){    while(scanf("%d",&n)!=EOF)    {        int i,j;        for(i=1;i<=n;i++)        {            scanf("%d",&val[i]);            dp[i][i]=n*val[i];        }        for(i=n-1;i>=1;i--)            for(j=i;j<=n;j++)            {                int fac=n-(j-i);                dp[i][j]=max(dp[i+1][j]+fac*val[i],dp[i][j-1]+fac*val[j]);            }        printf("%d\n",dp[1][n]);    }    return 0;}