POJ 3186 Treats for the Cows 一个简单DP
DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了
稍微想一下,就可以,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #include<cstring> #include<vector> #include<queue> #include<stack> using namespace std; int v[2005]; int dp[2005][2005]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&v[i]); memset(dp,0,sizeof(dp)); for(int k=n-1;k>=1;--k) { for(int i=1;i+k-1<=n;++i) { if(i>1) dp[i][i+k-1]=max(dp[i][i+k-1],dp[i-1][i+k-1]+v[i-1]*(n-k)); if(i+k-1<n) dp[i][i+k-1]=max(dp[i][i+k-1],dp[i][i+k]+v[i+k]*(n-k)); } } int ans=0; for(int i=1;i<=n;i++) ans=max(ans,dp[i][i]+v[i]*n); printf("%d\n",ans); return 0; }