# UVA 10559 Blocks

#include    <iostream>
#include    <cstdio>
#include    <cstdlib>
#include    <algorithm>
#include    <vector>
#include    <cstring>
#include    <queue>
#include    <complex>
#include    <stack>
#define LL long long int
#define dob double
#define FILE "10559"
using namespace std;

const int N = 221;
int n,A[N],f[N][N][N];

inline int gi(){
int x=0,res=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')res*=-1;ch=getchar();}
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*res;
}

inline int dfs(int i,int j,int k,register int r=0){
if(f[i][j][k])return f[i][j][k];
for(r=j;r>=i && A[r]==A[j];--r);int p=(k+j-r)*(k+j-r);
if(r<i)return f[i][j][k]=p;f[i][j][k]=dfs(i,r,0)+p;
for(register int l=r;l>=i;--l)
if(A[l]==A[j])
f[i][j][k]=max(f[i][j][k],dfs(i,l,k+j-r)+dfs(l+1,r,0));
return f[i][j][k];
}

inline void solve(){
n=gi();memset(f,0,sizeof(f));
for(int i=1;i<=n;++i)A[i]=gi();
for(int i=1;i<=n;++i)
for(int j=0;j<n;++j)
f[i][i][j]=(j+1)*(j+1);
printf("%d\n",dfs(1,n,0));
}

int main()
{
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
int Case=gi();
for(int t=1;t<=Case;++t){
printf("Case %d: ",t);
solve();
}
fclose(stdin);fclose(stdout);
return 0;
}
Blocks

posted @ 2017-10-16 23:06  Fenghr  阅读(347)  评论(1编辑  收藏  举报