POJ – 3186 Treats for the Cows

题目链接

f[i][j]:只剩第i个到第j个时,将其取完获得的最大价值.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int INF = 1e9;

int f[2005][2005];
int a[2005];
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int n;
  cin>>n;
  for(int i=1;i<=n;++i){
    cin>>a[i];
    f[i][i]=n*a[i];
  }
  for(int i=1;i<n;++i){
      for(int l=1;l+i<=n;++l){
        int r=l+i;
        f[l][r]=max(f[l+1][r]+(n-i)*a[l],f[l][r-1]+(n-i)*a[r]);
      }
  }
  cout<<f[1][n]<<endl;
  return 0;
}