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



Sample input:
2
1 2 1 2 2 1


Sample output:
2

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(i,x,y):
        if mem[(i,x,y)] is not None:
            return mem[(i,x,y)]
        cnt = {}
        f = [1]*5
        res = 0
        c = [x,y]+a[i+2:i+5]
        for b in c:
            if b in cnt:
                cnt[b] += 1
                if cnt[b] >= 3:
                    res += 1
                    for j in range(5):
                        if c[j] == b:
                            f[j] = 0
                            cnt[b] -= 1
                        if cnt[b] < 1:
                            break
                    break
            else:
                cnt[b] = 1
        if i == 3*(n-1):
            return res
        m = 0
        for k in range(5):
            if not f[k]:
                continue
            for j in range(k):
                if not f[j]:
                    continue
                r = dfs(i+3, a[i+j], a[i+k])
                if m < r:
                    m = r
        res += m
        mem[(i,x,y)] = res
        return res
    n = I()
    a = LI()
    mem = defaultdict(lambda : None)
    print(dfs(0,a[0],a[1]))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
b=[]
for i in range(n-1):
    b.clear()
    b=(list(map(int,a[0:5])))
    for j in range(1,n+1):
        if b.count(j)==3:
            ans+=1
            a.remove(j)
            a.remove(j)
            a.remove(j)
a.remove(a[0])
a.remove(a[1])
if sum(a)%a[0]==0:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))

    def enc(a, b):
        return a*(N+1)+b

    ans = [[-N*10] * (N+1) for _ in range(N+1)]
    ma = [-N*10] * (N+1)
    a0, a1 = A[0], A[1]
    ans[a0][a1] = 0
    ans[a1][a0] = 0
    ma[a0] = 0
    ma[a1] = 0
    mama = 0
    add = 0
    for i in range(N-1):
        a, b, c = A[3*i+2:3*i+5]
        if a == b == c:
            add += 1
            continue

        Q = {}
        # choose 2
        # ab
        for j in range(1, N+1):
            val1 = ans[c][c] + 1
            val2 = mama
            val = max(val1, val2)
            ab = enc(a, b)
            if ab not in Q:
                Q[ab] = [val]
            else:
                Q[ab].append(val)
        # bc
        for j in range(1, N + 1):
            val1 = ans[a][a] + 1
            val2 = mama
            val = max(val1, val2)
            bc = enc(b, c)
            if bc not in Q:
                Q[bc] = [val]
            else:
                Q[bc].append(val)
        # ca
        for j in range(1, N+1):
            val1 = ans[b][b] + 1
            val2 = mama
            val = max(val1, val2)
            ac = enc(a, c)
            if ac not in Q:
                Q[ac] = [val]
            else:
                Q[ac].append(val)
        # choose 1
        # a
        if b == c:
            flg = 1
        else:
            flg = 0
        for j in range(1, N+1):
            val = ma[j]
            if flg:
                val = max(val, ans[b][j] + 1)
            aj = enc(a, j)
            if aj not in Q:
                Q[aj] = [val]
            else:
                Q[aj].append(val)
        # b
        if a == c:
            flg = 1
        else:
            flg = 0
        for j in range(1, N + 1):
            val = ma[j]
            if flg:
                val = max(val, ans[a][j] + 1)
            bj = enc(b, j)
            if bj not in Q:
                Q[bj] = [val]
            else:
                Q[bj].append(val)
        # c
        if b == a:
            flg = 1
        else:
            flg = 0
        for j in range(1, N + 1):
            val = ma[j]
            if flg:
                val = max(val, ans[a][j] + 1)
            cj = enc(c, j)
            if cj not in Q:
                Q[cj] = [val]
            else:
                Q[cj].append(val)

        # update
        for key in Q:
            val = max(Q[key])
            i, j = divmod(key, N+1)
            ans[i][j] = val
            ans[j][i] = val
            ma[i] = max(ma[i], val)
            ma[j] = max(ma[j], val)
            mama = max(mama, val)
    ANS = 0
    for i in range(1, N+1):
        for j in range(1, N+1):
            if i == j == A[-1]:
                ANS = max(ANS, ans[i][j] + 1)
            else:
                ANS = max(ANS, ans[i][j])
    print(ANS + add)
    # print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
result = []
for k in set(arr):
    result.append(arr.count(k) // 3)
z = sum(result)
if z < n:
    print(z * (n-1))
else:
    print(z)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
result = []
for k in set(arr):
    result.append(arr.count(k) // 3)
print(sum(result))

Submission status: Wrong Answer

Solution code:
n=int(input())
array = list(map(int, input().strip().split()))
s=list(set(array))
l=len(s)
f=0
for i in range(l):
    f+=array.count(s[i])//3
print(f)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
D = {0: [[0] * (N + 1)]}
D[0][0][A[0]] += 1
D[0][0][A[1]] += 1

ma = 0
for i in range(N):
    nD = {}
    for x in (ma-1, ma):
        if x not in D: continue
        for y in D[x]:
            y = y[:]
            B = A[i*3+2:i*3+5]
            for a in B:
                y[a] += 1
            if x in nD:
                nD[x].append([min(2, t) for t in y])
            else:
                nD[x] = [[min(2, t) for t in y]]
            for b in set(B):
                if y[b] >= 3:
                    ma = max(ma, x + 1)
                    yy = y[:]
                    for i in range(N+1):
                        if i != b and yy[i] >= 3:
                            yy[i] = 2
                    cnt = B.count(b)
                    if cnt == 3:
                        yy[b] -= 3
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    elif cnt == 2:
                        an = sum(B) - 2 * b
                        yy[b] -= 3
                        for i in range(N+1):
                            if i == an:
                                if yy[i] > 2: yy[i] = 2
                            else:
                                if yy[i] > 1: yy[i] = 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    else:
                        if b == B[0]:
                            an1, an2 = B[1], B[2]
                        elif b == B[1]:
                            an1, an2 = B[0], B[2]
                        else:
                            an1, an2 = B[0], B[1]
                        yy[b] -= 3
                        yy = [0] * (N+1)
                        yy[an1] += 1
                        yy[an2] += 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                        
    
    D = nD
print(ma)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dfs(i,x,y):
        if mem[(i,x,y)] is not None:
            return mem[(i,x,y)]
        cnt = {}
        f = [1]*5
        res = 0
        c = [x,y]+a[i+2:i+5]
        for b in c:
            if b in cnt:
                cnt[b] += 1
                if cnt[b] >= 3:
                    res += 1
                    for j in range(5):
                        if c[j] == b:
                            f[j] = 0
                            cnt[b] -= 1
                        if cnt[b] < 1:
                            break
                    break
            else:
                cnt[b] = 1
        if i == 3*(n-1):
            return res
        m = 0
        for k in range(5):
            if not f[k]:
                continue
            for j in range(k):
                if not f[j]:
                    continue
                r = dfs(i+3, a[j], a[k])
                if m < r:
                    m = r
        res += m
        mem[(i,x,y)] = res
        return res
    n = I()
    a = LI()
    mem = defaultdict(lambda : None)
    print(dfs(0,a[0],a[1]))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = [val - 1 for val in a]


dp = {}
dq = {}

a0 = a[0]
a1 = a[1]
if a0 > a1:
    a0, a1 = a1, a0
dp[a0, a1] = 0


for i in range(n - 1):
    l = i * 3 + 2
    r = i * 3 + 5
    cand = a[l:r]

    
    for a0, a1 in dp:
        vals = [a0, a1] + cand
        cnt = {}
        max_cnt = 0

        for val in vals:
            cnt[val] = cnt.get(val, 0) + 1
            max_cnt = max(max_cnt, cnt[val])

        if max_cnt == 3:
            tmp = []
            for val in cnt:
                if cnt[val] != 3:
                    tmp.append(val)
            tmp.sort()
            tmp.append(tmp[-1])
            dq[tmp[0], tmp[1]] = dp[a0, a1] + 1
            continue
            
        vals.sort()
        for aii in range(5):
            for ajj in range(aii + 1, 5):
                ai = vals[aii]
                aj = vals[ajj]
                dq[ai, aj] = dp[a0, a1]
   
    dp, dq = dq, dp
    dq = {}

ans = 0
for a0, a1 in dp:
    if a0 == a[-1] and a1 == a[-1]:
        ans = max(dp[a0, a1] + 1, ans)
    else:
        ans = max(dp[a0, a1], ans)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50



def run():
    N, *A = map(int, read().split())
    A = A[::-1]

    dic = defaultdict(lambda:0)
    for a in A:
        dic[a] += 1

    score = 0
    while len(A) > 3:
        #print(A)
        tmp_dic = defaultdict(lambda:0)
        for i in range(5):
            tmp_a = A.pop()
            tmp_dic[tmp_a] += 1
        done = False
        for key, val in tmp_dic.items():
            if val == 3:
                score += 1
                del tmp_dic[key]
                done = True
                dic[key] -= 3
                break
            elif val > 3:
                score += 1
                tmp_dic[key] -= 3
                done = True
                dic[key] -= 3
                break

        if not done:
            lis = []
            for key in tmp_dic.keys():
                lis.append((dic[key], key))
            lis = sorted(lis, key = lambda x:x[0])
            cnt = 0
            #print(lis)
            for _, key in lis:
                if 3 - cnt > tmp_dic[key]:
                    cnt += tmp_dic[key]
                    dic[key] -= tmp_dic[key]
                    del tmp_dic[key]
                else:
                    dic[key] -= 3-cnt
                    tmp_dic[key] -= 3-cnt
                    cnt = 3

        for key, val in tmp_dic.items():
            for _ in range(val):
                A.append(key)
    if A[0] == A[1] and A[1] == A[2]:
        score += 1

    print(score)


if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
D = {0: [[0] * (N + 1)]}
D[0][0][A[0]] += 1
D[0][0][A[1]] += 1

ma = 0
for i in range(N):
    nD = {}
    for x in (ma, ma-1):
        if x not in D: continue
        for y in D[x]:
            y = y[:]
            B = A[i*3+2:i*3+5]
            for a in B:
                y[a] += 1
            if x in nD:
                nD[x].append([min(2, t) for t in y])
            else:
                nD[x] = [[min(2, t) for t in y]]
            for b in set(B):
                if y[b] >= 3:
                    ma = max(ma, x + 1)
                    yy = y[:]
                    for i in range(N+1):
                        if i != b and yy[i] >= 3:
                            yy[i] = 2
                    cnt = B.count(b)
                    if cnt == 3:
                        yy[b] -= 3
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    elif cnt == 2:
                        an = sum(B) - 2 * b
                        yy[b] -= 3
                        for i in range(N+1):
                            if i == an:
                                if yy[i] > 2: yy[i] = 2
                            else:
                                if yy[i] > 1: yy[i] = 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    else:
                        if b == B[0]:
                            an1, an2 = B[1], B[2]
                        elif b == B[1]:
                            an1, an2 = B[0], B[2]
                        else:
                            an1, an2 = B[0], B[1]
                        yy[b] -= 3
                        yy = [0] * (N+1)
                        yy[an1] += 1
                        yy[an2] += 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                        
    
    D = nD
print(ma)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=0
for i in range(N-1):
    B=[]
    C=[]
    for j in range(5):
        B.append(A[0])
        A.pop(0)
    for i in range(5):
        cnt=0
        if B[i] not in B[:i]:
            cnt=B.count(B[i])
            C.append((cnt,B[i]))
    C=sorted(C)[::-1]
    check=[]
    for j in range(len(C)):
        for k in range(C[j][0]):
            check.append(C[j][1])
    if check[0]==check[1] and check[1]==check[2]:
        ans+=1
    A.insert(0,check[3])
    A.insert(1,check[4])
if len(set(A))==1:
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
D = {0: [[0] * (N + 1)]}
D[0][0][A[0]] += 1
D[0][0][A[1]] += 1

ma = 0
for i in range(N):
    nD = {}
    for x in (ma, ma-1):
        if x not in D: continue
        for y in D[x]:
            B = A[i*3+2:i*3+5]
            for a in B:
                y[a] += 1
            if x in nD:
                nD[x].append([min(2, t) for t in y])
            else:
                nD[x] = [[min(2, t) for t in y]]
            for b in set(B):
                if y[b] >= 3:
                    ma = max(ma, x + 1)
                    yy = y[:]
                    for i in range(N+1):
                        if i != b and yy[i] >= 3:
                            yy[i] = 2
                    cnt = B.count(b)
                    if cnt == 3:
                        yy[b] -= 3
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    elif cnt == 2:
                        an = sum(B) - 2 * b
                        yy[b] -= 3
                        for i in range(N+1):
                            if i == an:
                                if yy[i] > 2: yy[i] = 2
                            else:
                                if yy[i] > 1: yy[i] = 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    else:
                        if b == B[0]:
                            an1, an2 = B[1], B[2]
                        elif b == B[1]:
                            an1, an2 = B[0], B[2]
                        else:
                            an1, an2 = B[0], B[1]
                        yy[b] -= 3
                        yy = [0] * (N+1)
                        yy[an1] += 1
                        yy[an2] += 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                        
    
    D = nD
print(ma)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()] + [0] * 2
D = {0: [[0] * (N + 1)]}
D[0][0][A[0]] += 1
D[0][0][A[1]] += 1

ma = 0
for i in range(N):
    nD = {}
    for x in (ma, ma-1):
        if x not in D: continue
        for y in D[x]:
            B = A[i*3+2:i*3+5]
            for a in B:
                y[a] += 1
            if x in nD:
                nD[x].append([min(2, t) for t in y])
            else:
                nD[x] = [[min(2, t) for t in y]]
            for b in set(B):
                if y[b] >= 3:
                    ma = x + 1
                    yy = y[:]
                    for i in range(N+1):
                        if i != b and yy[i] >= 3:
                            yy[i] = 2
                    cnt = B.count(b)
                    if cnt == 3:
                        yy[b] -= 3
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    elif cnt == 2:
                        an = sum(B) - 2 * b
                        yy[b] -= 3
                        for i in range(N+1):
                            if i == an:
                                if yy[i] > 2: yy[i] = 2
                            else:
                                if yy[i] > 1: yy[i] = 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                    else:
                        if b == B[0]:
                            an1, an2 = B[1], B[2]
                        elif b == B[1]:
                            an1, an2 = B[0], B[2]
                        else:
                            an1, an2 = B[0], B[1]
                        yy[b] -= 3
                        yy = [0] * (N+1)
                        yy[an1] += 1
                        yy[an2] += 1
                        if x+1 in nD:
                            nD[x+1].append(yy)
                        else:
                            nD[x+1] = [yy]
                        
    
    D = nD
print(ma)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()


n = ni()
a = na()

dp = [[-99999999] * (n+1) for _ in range(n+1)]
dp[a[0]][a[1]] = 0

dprow = [-99999999] * (n+1)
dpcol = [-99999999] * (n+1)
dprow[a[0]] = 0
dpcol[a[1]] = 0

margin = 0
for i in range(2, 3*n-2, 3):
    if a[i] == a[i+1] and a[i+1] == a[i+2]:
        margin += 1
    else:
        q = []
        for j in (a[i], a[i+1], a[i+2]):
            for k in (a[i], a[i+1], a[i+2]):
                b = [j, k, a[i], a[i+1], a[i+2]]
                b.sort()
                if b[0] == b[2]:
                    q.append((b[3], b[4], dp[j][k] + 1))
                if b[1] == b[3]:
                    q.append((b[0], b[4], dp[j][k] + 1))
                if b[2] == b[4]:
                    q.append((b[0], b[1], dp[j][k] + 1))
        for j in range(n+1):
            for k in (a[i], a[i + 1], a[i + 2]):
                q.append((j, k, dprow[j]))
                q.append((k, j, dpcol[j]))
        mx = max(dprow)
        for jj in range(i, i+3):
            for kk in range(jj+1, i+3):
                q.append((a[jj], a[kk], mx))
        for u in q:
            dp[u[0]][u[1]] = max(dp[u[0]][u[1]], u[2])
            dprow[u[0]] = max(dprow[u[0]], u[2])
            dpcol[u[1]] = max(dpcol[u[1]], u[2])

ans = max(*dprow, dp[a[-1]][a[-1]] + 1)

print(ans+margin)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

N = int(input())
A = [int(i) for i in input().split()]

ret = 0
dp = [0] * (N + 1)
for i in range(2) :
  dp[A[i]] += 1

i = 2
while i < 3 * N :
  a = set()
  for j in range(3) :
    if i + j < 3 * N :
      dp[A[i + j]] += 1
      a.add(A[i + j])
  i += 3
  for j in range(N + 1) :
    if dp[j] >= 3 :
      ret += 1
      dp[j] -= 3
      a.remove(j)
      if len(a) == 2 :
        dp = [0] * (N + 1)
      elif len(a) == 1 :
        for j in range(N + 1) :
          dp[j] = min(dp[j], 1)
      for aa in a :
        dp[aa] += 1
      break
  else :
    for j in range(N + 1) :
      dp[j] = min(dp[j], 2)
          
print(ret)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
d=[0]*(n+1)
ans=0
for x in a[:5]:
  d[x]+=1
m,mi=0,0
for j in range(1,n+1):
  if m<d[j]:
    m=d[j]
    mi=j
if m>=3:
  ans+=1
  d=[0]*(n+1)
  d[mi]=0
  ans+=1

for i in range(1,n):
  m,mi=0,0
  for j in range(1,n+1):
    if m<d[j]:
      m=d[j]
      mi=j
  if m>=3:
    ans+=1
    d=[0]*(n+1)
    d[a[(i)*3-1]]+=1
    d[a[(i)*3]]+=1
    d[a[(i)*3+1]]+=1
    d[mi]-=1
  else:
    d[a[(i)*3-1]]+=1
    d[a[(i)*3]]+=1
    d[a[(i)*3+1]]+=1
print(ans)

Submission status: Wrong Answer

Solution code:
mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    N = int(input())
    A = list(map(int, input().split()))
    N3 = 3*N

    def enc(a, b):
        return a*(N+1)+b

    ans = [[-N-1] * (N+1) for _ in range(N+1)]
    ma = [-N-1] * (N+1)
    a0, a1 = A[0], A[1]
    ans[a0][a1] = 0
    ans[a1][a0] = 0
    ma[a0] = 0
    ma[a1] = 0
    mama = 0
    add = 0
    for i in range(N-1):
        a, b, c = A[3*i+2:3*i+5]
        if a == b == c:
            add += 1
            continue

        Q = {}
        # choose 2
        # ab
        for j in range(1, N+1):
            val1 = ans[c][c] + 1
            val2 = mama
            val = max(val1, val2)
            ab = enc(a, b)
            ba = enc(b, a)
            if ab not in Q:
                Q[ab] = val
            else:
                Q[ab] = max(Q[ab], val)
        # bc
        for j in range(1, N + 1):
            val1 = ans[a][a] + 1
            val2 = mama
            val = max(val1, val2)
            bc = enc(b, c)
            cb = enc(c, b)
            if bc not in Q:
                Q[bc] = val
            else:
                Q[bc] = max(Q[bc], val)
        # ca
        for j in range(1, N+1):
            val1 = ans[b][b] + 1
            val2 = mama
            val = max(val1, val2)
            ac = enc(a, c)
            ca = enc(c, a)
            if ac not in Q:
                Q[ac] = val
            else:
                Q[ac] = max(Q[ac], val)
        # choose 1
        # a
        if b == c:
            flg = 1
        else:
            flg = 0
        for j in range(1, N+1):
            val = ma[j]
            if flg:
                val = max(val, ans[b][j] + 1)
            aj = enc(a, j)
            ja = enc(j, a)
            if aj not in Q:
                Q[aj] = val
            else:
                Q[aj] = max(Q[aj], val)
        # b
        if a == c:
            flg = 1
        else:
            flg = 0
        for j in range(1, N + 1):
            val = ma[j]
            if flg:
                val = max(val, ans[a][j] + 1)
            bj = enc(b, j)
            jb = enc(j, b)
            if bj not in Q:
                Q[bj] = val
            else:
                Q[bj] = max(Q[bj], val)
        # c
        if b == a:
            flg = 1
        else:
            flg = 0
        for j in range(1, N + 1):
            val = ma[j]
            if flg:
                val = max(val, ans[a][j] + 1)
            cj = enc(c, j)
            jc = enc(j, c)
            if cj not in Q:
                Q[cj] = val
            else:
                Q[cj] = max(Q[cj], val)

        # update
        for key in Q:
            val = Q[key]
            i, j = divmod(key, N+1)
            ans[i][j] = val
            ans[j][i] = val
            ma[i] = max(ma[i], val)
            ma[j] = max(ma[j], val)
            mama = max(mama, val)
    ANS = 0
    for i in range(1, N+1):
        for j in range(1, N+1):
            if i == j == A[-1]:
                ANS = max(ANS, ans[i][j] + 1)
            else:
                ANS = max(ANS, ans[i][j])
    print(ANS + add)
    # print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

tmp = A[:5]
tmp.append(A[-1])
_, counts = zip(*Counter(tmp).most_common())

cnt = 0
cnt += counts.count(3)
for i in range(5, 3*N-3, 3):
    if len(set(A[i:i+3]))==1:
        cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque,Counter
    import sys
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    A=deque(A)
    score=0
    for _ in range(N-1):
        lst=[0]*5
        for i in range(5):
            lst[i]=A.popleft()
        
        flag=False
        C=Counter(lst)    
        for key,val in C.items():
            if val==3:
                score+=1
                for a in lst:
                    if a!=key:
                        A.appendleft(a)
                flag=True
                break
        
        if not flag:
            k1=0
            for a in A:
                if a in C:
                    C[a]+=1
                    if C[a]==3:
                        k1=a
                        break
                    
            if k1 and lst.count(k1)==2:
                A.appendleft(k1)
                A.appendleft(k1)#2ついれる

            elif k1:
                A.appendleft(k1)
                for a in C:
                    if C[a]==2:
                        C[a]=1
                
                k2=0
                for a in A:
                    if a in C:
                        C[a]+=1
                        if C[a]==3 and a!=k1:
                            k2=a
                            break
                if k2:
                    A.appendleft(k2)
                else:
                    A.appendleft(lst[0])       
                    
            else:
                A.extend(lst[:2])
    
    if A[0]==A[1]==A[2]:
        print(score+1)
    else:
        print(score)

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

N = int(input())
A = [int(i) for i in input().split()]

ret = 0
dp = [0] * (N + 1)
for i in range(2) :
  dp[A[i]] += 1

i = 2
while i < 3 * N :
  a = set()
  for j in range(3) :
    if i + j < 3 * N :
      dp[A[i + j]] += 1
      a.add(A[i + j])
  i += 3
  for j in range(N + 1) :
    if dp[j] >= 3 :
      ret += 1
      dp[j] -= 3
      a.remove(j)
      if len(a) == 2 :
        dp = [0] * (N + 1)
      elif len(a) == 1 :
        for j in range(N + 1) :
          dp[j] = min(dp[j], 1)
      for aa in a :
        dp[aa] += 1
      break
          
print(ret)