题意:给出的一系列的数字,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最大和
思路:由里向外逆推区间,一上来初始化,完了再一层一层区间dp上去。
#include <cstdio>#include <iostream>#include <vector>#include <cmath>#include <set>#include <map>#include <queue>#include <algorithm>#include <cstring>#include <string>using namespace std;int a[2010];int dp[2010][2010];int main(){ int n; while (scanf ("%d",&n)!=EOF) { for (int i=1;i<=n;i++) scanf ("%d",&a[i]); for (int i=1;i<=n;i++) dp[i][i]=n*a[i]; int ans=0; for (int l=2;l<=n;l++) { for (int i=1;i+l-1<=n;i++) { int j=i+l-1; dp[i][j]=max(dp[i][j-1]+(n-l+1)*a[j],dp[i+1][j]+(n-l+1)*a[i]); } } printf ("%d\n",dp[1][n]); } return 0;}