题目链接:http://poj.org/problem?id=3186
题目大意:一个数列,可以从两端取数,取出后乘以自己是第几个被取的加和求最大值
题目思路:一共两种情况,要么从左边取要么从右边取,定义dp[i][j]是左边取i个右边取j个的最大值,那么dp[i][j]=max(dp[i-1][j]+(i+j)*a[i],dp[i][j-1]+(i+j)*a[n-j+1]
以下是代码:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
const int MAXN = 2005;
int a[MAXN],n;
ll dp[MAXN][MAXN];
int main(){
while(~scanf("%d",&n)){
rep(i,1,n)scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
ll ans=0;
rep(i,0,n){
rep(j,0,n){
if(i&&j){
dp[i][j]=max(dp[i-1][j]+(ll)(i+j)*a[i],dp[i][j-1]+(ll)(i+j)*a[n-j+1]);
continue;
}
if(i)dp[i][j]=dp[i-1][j]+(ll)(i+j)*a[i];
if(j)dp[i][j]=dp[i][j-1]+(ll)(i+j)*a[n-j+1];
}
}
rep(i,0,n){
ans=max(dp[i][n-i],ans);
}
printf("%lld\n",ans);
}
return 0;
}