就是石子合并,贡献不一样罢了
贡献为
直接set 算
就行了
rep(i,1,n*2){
set<int> s;
rep(j,i,n*2)s.insert(a[j]),sum[i][j]=s.size();
}
Code
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<string>
#include<bitset>
#include<vector>
#include<cstdio>
#include<complex>
#include<cstdlib>
#include<climits>
#include<iomanip>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#define int long long
#define pb(x) push_back(x)
#define lowbit(x) x&(-x)
#define mkp make_pair
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define fan putchar('\n')
#define fak putchar(' ')
//c++11 [-std=c++11]
//#pragma GCC optimize(2,3,"Ofast","inline")
using namespace std;
inline int read() {
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
}
inline void write(int X) {
if(X<0) {X=~(X-1); putchar('-');}
if(X>9) write(X/10);
putchar(X%10+'0');
}
#define mod 998244353
#define N 400*2
int n,a[N];
int f[N][N];
int sum[N][N];
int mx;
void solve(){
//别挂了
n=read();
rep(i,1,n)a[i]=a[i+n]=read();
rep(i,1,n*2){
set<int> s;
rep(j,i,n*2){
s.insert(a[j]);
sum[i][j]=s.size();
}
}
rep(l,1,n-1)for(int i=1,j=i+l;(j<n+n)&&(i<n+n);i++,j=i+l)rep(k,i,j-1)f[i][j]=max(f[i][j],f[i][k]+f[k+1][j]+sum[i][k]*sum[k+1][j]);
rep(i,1,n)mx=max(mx,f[i][i+n-1]);
write(mx);
}
signed main(){
solve();
return 0;
}