Language: C++

#include<cstdio>
#define N 6001
using namespace std;
int f[N][N],wei[N],last[N],before[N],sbefore[N],right[N];
int first[N][N],second[N][N];
int a[N];
int n;
bool pd(int x,int y)
{
	if (sbefore[x]==-1||sbefore[y]==-1) return false;
	if (x==0||y==0) return true;
	if (x==y) return true;	
	if (wei[x]==wei[y]) return false;
	if (last[x]==y||sbefore[x]==y) return false;
	if (last[y]==x||sbefore[y]==x) return false;
	if (wei[x]<=wei[sbefore[y]]||(wei[y]<=wei[sbefore[x]])) return true;
	if (wei[x]!=wei[last[x]]&&wei[sbefore[y]]<wei[x]&&wei[y]>wei[last[x]])
	{
		if (!(wei[y]==wei[last[y]]&&wei[last[y]]==wei[last[last[y]]])) return false;
		//return true;	
	}
	if (wei[y]!=wei[last[y]]&&wei[sbefore[x]]<wei[y]&&wei[x]>wei[last[y]])
	{
		if (!(wei[x]==wei[last[x]]&&wei[last[x]]==wei[last[last[x]]])) return false;
		///return true;
	}	
	return true;
}
int max(int x,int y)
{
	if (x<y) return y;
	return x;
}
int main()
{
	scanf("%d",&n);
	n*=3;
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n-5;i++) wei[i+5]=(i-1)/3+2;
	for (int i=1;i<=5;i++) wei[i]=1;
	for (int i=1;i<=n;i++)
		if (wei[i]!=wei[i+1]) right[wei[i]]=i;
	right[wei[n]]=n;
	for (int i=1;i<=n;i++) before[i]=-1;
	for (int i=1;i<=n;i++)
	{
		last[i]=before[a[i]];
		before[a[i]]=i;
	}
	for (int i=1;i<=n;i++) 
		if (last[i]==-1) sbefore[i]=-1;
		else sbefore[i]=last[last[i]];
	for (int i=0;i<=n;i++)
		for (int j=0;j<=n;j++)
		{
			if (i==18&&j==18)
			{
				n++;n--;
			}
			if (pd(i,j))
			{
				f[i][j]=2;
				if (i==0) f[i][j]--;
				if (j==0) f[i][j]--;
				if (i!=0&&i==j) f[i][j]--;
			}
			else	f[i][j]=-1e9;
		}
	bool bo=pd(26,27);
	for (int i=1;i<=n;i++) first[0][i]=f[0][i];
	for (int i=1;i<=n;i++)
	{
		second[i][0]=f[i][0];
		for (int j=1;j<=n;j++)
			if (pd(i,j))
			{
				if (i==1434&&j==1490)
				{
					n++;n--;
				}
				int r=sbefore[i]-1;
				if (r>=i) r=i-1;
				if (i==j)
				{
					f[i][j]=first[r][j];
					for (int k=sbefore[i]+1;k<=right[wei[sbefore[i]]];k++)
						f[i][j]=max(f[i][j],f[k][j]);
				}
				if (i>j)
				{
					r=j;
					if (r>i-1) r=i-1;
					if (r>sbefore[i]-1)
						r=sbefore[i]-1;
					for (int k=sbefore[i]+1;k<=right[wei[sbefore[i]]];k++)
						f[i][j]=max(f[i][j],f[k][j]+1);
					f[i][j]=max(f[i][j],first[r][j]+1);
					for (int k=r+1)
				}
				if (i<j)
				{
					r=i;
					if (r>j-1) r=j-1;
					if (r>sbefore[j]) r=sbefore[j]-1;
					for (int k=sbefore[j]+1;k<=right[wei[sbefore[j]]];k++)
						f[i][j]=max(f[i][j],f[i][k]+1);
					f[i][j]=max(f[i][j],second[i][r]+1);
				}
				second[i][j]=max(f[i][j],second[i][j-1]);
			}
			else	second[i][j]=second[i][j-1];
		for (int j=1;j<=n;j++)
			first[i][j]=max(f[i][j],first[i-1][j]);
	}
	int max=0;
	for (int i=0;i<=n;i++)
		for (int j=1;j<=n;j++)
		{
			if (max<f[i][j]) max=f[i][j];
			if (i==1434&&j==1490)
			{
				n++;n--;
			}
			if (max==5)
			{
				max++;max--;
			}
		}
	printf("%d\n",max);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:98:55: error: expected ';' before ')' token
   98 |                                         for (int k=r+1)
      |                                                       ^
      |                                                       ;
a.cc:98:55: error: expected primary-expression before ')' token
a.cc:98:55: error: expected ';' before ')' token
   98 |                                         for (int k=r+1)
      |                                                       ^
      |                                                       ;
a.cc:99:33: error: expected primary-expression before '}' token
   99 |                                 }
      |                                 ^