Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,int> pi;
typedef pair<lld,lld> pl;
typedef vector<int> vit;
typedef vector<vit> vitt;
typedef vector<lld> vlt;
typedef vector<vlt> vltt;
typedef vector<pi> vpit;
typedef vector<vpit> vpitt;
typedef long double ld;
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mk(a,b) make_pair(a,b)
bool isrange(int y,int x,int n,int m){
	 if(0<=y&&y<n&&0<=x&&x<m) return true;
	 return false;
}
int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1};
const int MAX = 2020;
int dp[MAX][MAX],arr[MAX*3];
int main(void){
	for(int e=0;e<MAX;e++) for(int p=0;p<MAX;p++) dp[e][p] = -1e9;
	int n;
	scanf("%d",&n);
	for(int e=0;e<3*n;e++) scanf("%d",&arr[e]);
	if(n==1){
		int ll = -1,err = 0;
		for(int e=0;e<3;e++){
			if(ll==-1) ll = arr[e];
			else{
				if(ll!=arr[e]) err = 1;
			}
		}
		if(err) printf("0");
		else printf("1");
		return 0;
	}
	for(int e=0;e<5;e++){
		for(int p=0;p<5;p++){
			if(e==p) continue;
			int ll = -1,err = 0;
			for(int q=0;q<5;q++){
				if(e==q||p==q) continue;
				if(ll==-1) ll = arr[q];
				else{
					if(ll!=arr[q]) err = 1;
				}
			}
			if(err==0){
				dp[min(arr[e],arr[p])][max(arr[e],arr[p])] = 1;
			}else{
				dp[min(arr[e],arr[p])][max(arr[e],arr[p])] = max(dp[min(arr[e],arr[p])][max(arr[e],arr[p])],0);
			}
		}
	}
	int tt = 0;
	for(int e=1;e<=n-2;e++){
		int lx = 5 + (e-1) * 3, rx = 5 + e * 3;
		int ll = -1,err = 0,res = 0;
		for(int q=lx;q<rx;q++){
			if(ll==-1) ll = arr[q];
			else{
				if(ll!=arr[q]) err = 1;
			}
		}
		int res = 0;
		if(err==0){
			tt ++;
			res = 1;
		}
		vector<pair<pi,int> > v;
		for(int q=lx;q<rx;q++){
			if(dp[arr[q]][arr[q]]!=-1e9){
				int s1 = -1,s2 = -1;
				for(int r=lx;r<rx;r++){
					if(q==r) continue;
					if(s1==-1) s1 = arr[r];
					else s2 = arr[r];
				}
				if(s1>s2) swap(s1,s2);
				v.push_back(mk(mk(s1,s2),dp[arr[q]][arr[q]]+1 - res));
			}
			for(int r=q+1;r<rx;r++){
				if(arr[q]!=arr[r]) continue;
				int s1 = -1;
				for(int p=lx;p<rx;p++){
					if(q==p||r==p) continue;
					s1 = arr[p];
				}
				for(int p=1;p<=arr[q];p++){
					if(dp[p][arr[q]]==-1e9) continue;
					int s2 = p;
					int vv = dp[p][arr[q]] + 1 - res;
					v.push_back(mk(mk(min(s1,s2),max(s1,s2)),vv));
				}
				for(int p=arr[q]+1;p<=n;p++){
					if(dp[arr[q]][p]==-1e9) continue;
					int s2 = p;
					int vv = dp[arr[q]][p] + 1 - res;
					v.push_back(mk(mk(min(s1,s2),max(s1,s2)),vv));
				}
			}
		}
		for(int e=0;e<sz(v);e++){
			int f1 = v[e].x.x;
			int f2 = v[e].x.y;
			int wei = v[e].y;
			dp[f1][f2] = max(dp[f1][f2],wei);
		}
	}
	int lastn = arr[3*n - 1];
	if(dp[lastn][lastn]!=-1e9) dp[lastn][lastn]++;
	int ans = 0;
	for(int e=1;e<=n;e++) for(int p=1;p<=n;p++) if(dp[e][p]!=-1){
		ans = max(ans,dp[e][p] + tt);
	}
	printf("%d",ans);
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:70:21: error: redeclaration of 'int res'
   70 |                 int res = 0;
      |                     ^~~
a.cc:63:37: note: 'int res' previously declared here
   63 |                 int ll = -1,err = 0,res = 0;
      |                                     ^~~