Problem:
Score : 600 points

Problem Statement

We have 3N cards arranged in a row from left to right, where each card has an integer between 1 and N (inclusive) written on it. The integer written on the i-th card from the left is A_i.

You will do the following operation N-1 times:

Rearrange the five leftmost cards in any order you like, then remove the three leftmost cards. If the integers written on those three cards are all equal, you gain 1 point.

After these N-1 operations, if the integers written on the remaining three cards are all equal, you will gain 1 additional point.

Find the maximum number of points you can gain.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_{3N}

Output

Print the maximum number of points you can gain.

Sample Input 1

2
1 2 1 2 2 1

Sample Output 1

2

Let us rearrange the five leftmost cards so that the integers written on the six cards will be 2\ 2\ 2\ 1\ 1\ 1 from left to right.

Then, remove the three leftmost cards, all of which have the same integer 2, gaining 1 point.

Now, the integers written on the remaining cards are 1\ 1\ 1.

Since these three cards have the same integer 1, we gain 1 more point.

In this way, we can gain 2 points - which is the maximum possible.

Sample Input 2

3
1 1 2 2 3 3 3 2 1

Sample Output 2

1

Sample Input 3

3
1 1 2 2 2 3 3 3 1

Sample Output 3

3

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>


#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O2")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
#ifdef VAMP
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif

const int N = 6005;
int n , ara[N] , cnt[N] , loc[N];
int main()
{
    #ifdef VAMP
        clock_t tStart = clock();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    cin >> n;
    if(n <= 2){
        cout << n << "\n";
        return 0;
    }
    map<int,int>mp;
    int id = 0;
    FOR(i,1,3 * n){
        cin >> ara[i];
        cnt[ara[i]]++;
        if(cnt[ara[i]] == 3){
            mp[ara[i]] = ++id;
        }
    }
    FOR(i,1,3 * n)ara[i] = mp[ara[i]];

    int lo = 1 , hi = n , mid , ans;
    while(lo <= hi){
        mid = (lo + hi) >> 1;
        MEM(cnt,0);

        FOR(i,1,3 * n){
            if(ara[i] <= mid){
                cnt[ara[i]]++;
                if(cnt[ara[i]] == 3)loc[i] = 1;
            }
        }
        int save = 0,f = 0;
        for(int i = 1;i <= 5;i++){
            if(ara[i] <= mid)save++;
            if(loc[i] == 1)f = 1;
        }
        if(f)save = save - 3;

        if(save > 2){
            hi = mid - 1;
            continue;
        }

        bool ok = 1;
        int done = 5;
        for(int i = 8;i <= 3 * n;i += 3){
            bool found = 0;
            for(int j = done + 1;j <= i;j++){
                if(ara[j] <= mid)save++;
                if(loc[j] == 1)found = 1;
            }
            if(found){
                save = save - 3;
            }
            if(save > 2){
                ok = 0;
                break;
            }
            done = i;
        }
        if(!ok){
            hi = mid - 1;
            continue;
        }
        bool found = 0;
        for(int j = done + 1;j <= 3 * n;j++){
            if(ara[j] <= mid)save++;
            if(loc[j] == 1)found = 1;
        }

        if(found)save = save - 3;
        if(save != 0){
            ok = 0;
        }
        if(ok){
            ans = mid;
            lo = mid + 1;
        }else{
            hi = mid - 1;
        }
    }
    cout << ans << "\n";

    #ifdef VAMP
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
    #endif
}