#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int N = 2009;
int dp[N][N];
int a[N];
int main() {
// freopen("data.in", "r", stdin);
int n;
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
//dp[i][j]表示左边取了i个数,右边取了j个数
//故dp[i][j] = max(dp[i-1][j] + a[i]* (i+j), dp[i][j-1] + a[n-j+1]*(i+j)); 注意当ij为0的边界判断即可。
int ans = 0;
for(int i = 0; i <= n; i++) {
for(int j = 0; j+i <= n; j++) {
if(i == 0 && j == 0)
dp[i][j] = 0;
else if(j == 0)
dp[i][j] = dp[i-1][j] + a[i]*i;
else if(i == 0)
dp[i][j] = dp[i][j-1] + a[n-j+1]*j;
else
dp[i][j] = max(dp[i-1][j] + a[i]*(i+j),
dp[i][j-1] + a[n-j+1]*(i+j));
ans = max(ans, dp[i][j]);
}
}
cout<<ans<<endl;
return 0;
}
POJ 3186 Treats for the Cows(DP)
最新推荐文章于 2019-10-12 20:39:18 发布