我是傻逼,我啥也不会,这是我抄的。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, a, b, w[55], tot, dy[1005], g[55][55], f[55][55][55][55], va[55];
bool isAll(int i, int j, int l, int r){
for(int k=i; k<=j; k++)
if(w[k]<l || w[k]>r)
return false;
return true;
}
bool isAny(int i, int j, int l, int r){
for(int k=i; k<=j; k++)
if(w[k]>=l && w[k]<=r)
return true;
return false;
}
int F(int, int, int, int);
int G(int, int);
int F(int i, int j, int l, int r){
int &re=f[i][j][l][r];
if(re!=-1) return re;
re = 0x3f3f3f3f;
if(isAll(i, j, l, r)){
re = 0;
return 0;
}
if(!isAny(i, j, l, r)){
re = G(i, j);
return re;
}
for(int k=i; k<j; k++){
re = min(re, F(i,k,l,r)+F(k+1,j,l,r));
re = min(re, G(i,k)+F(k+1,j,l,r));
}
return re;
}
int G(int l, int r){
int &re=g[l][r];
if(re!=-1) return re;
re = 0x3f3f3f3f;
for(int i=1; i<=tot; i++)
for(int j=i; j<=tot; j++)
if(isAny(l, r, i, j)){
int u=va[j], v=va[i];
re = min(re, F(l, r, i, j) + a + b * (v - u) * (v - u));
}
return re;
}
int main(){
cin>>n>>a>>b;
for(int i=1; i<=n; i++){
scanf("%d", &w[i]);
dy[w[i]] = 1;
}
for(int i=1; i<=1000; i++)
if(dy[i]){
va[++tot] = i;
dy[i] = tot;
}
for(int i=1; i<=n; i++)
w[i] = dy[w[i]];
memset(f, -1, sizeof(f));
memset(g, -1, sizeof(g));
cout<<G(1, n)<<endl;
return 0;
}