1 #include<algorithm> 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 using namespace std; 6 #define N 105 7 int n,x[N],f[N][N],ans; 8 int main() 9 { 10 scanf("%d",&n); 11 for(int i=1;i<=n;i++) 12 scanf("%d",&x[i]); 13 for(int len=n;len>=0;len--) 14 for(int l=1,r;(r=l+len-1)<=n;l++){ 15 for(int k=1;k<=l-2;k++) 16 f[l][r]=max(f[l][r],f[k][r]+abs(x[l-1]-x[k])*(l-k)); 17 for(int k=r+2;k<=n;k++) 18 f[l][r]=max(f[l][r],f[l][k]+abs(x[k]-x[r+1])*(k-r)); 19 f[l][r]=max(f[l][r],f[l-1][r]+x[l-1]); 20 f[l][r]=max(f[l][r],f[l][r+1]+x[r+1]); 21 } 22 for(int i=1;i<=n;i++) 23 ans=max(ans,f[i][i-1]); 24 printf("%d\n",ans); 25 return 0; 26 }
原文:https://www.cnblogs.com/yjkhhh/p/9359752.html