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:
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]))
        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 = 0
for j in range(n+1):
    for k in range(n+1):
        if j == k and j == a[-1]:
            ans = max(ans, dp[j][k] + 1)
        else:
            ans = max(ans, dp[j][k])

print(ans+margin)

Submission status: Wrong Answer

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

score = 0
for i in range(0,N-1):
  #print(A)
  # 左から5個取得
  tmp = A[:5]
  #print(tmp)
  for j in range(0,N+1):
    cnt = tmp.count(j)
    #print(j,cnt)
    if cnt >= 3:
      tmp.remove(j)
      tmp.remove(j)
      tmp.remove(j)
      score = score + 1
      break

  tmp_next = A[5:6]
  #print(tmp_next)
  cnt = tmp.count(tmp_next)
  #cnt = 0
  if cnt >= 1:
    tmp.remove(tmp_next)
    tmp = tmp + [tmp_next]
    A = tmp + A[5:]
  else:
    if len(tmp) == 2:
      A = tmp + A[5:]
      #print(A)
    else:
      A  =tmp[3:5] + A[5:]
      #print(A)

for k in range(0,N+1):
    cnt = A.count(k)
    #print(j,cnt)
    if cnt == 3:
      score = score + 1

print(score)

Submission status: Wrong Answer

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

score = 0
for i in range(0,N-1):
  #print(A)
  # 左から5個取得
  tmp = A[:5]
  #print(tmp)
  for j in range(0,N+1):
    cnt = tmp.count(j)
    #print(j,cnt)
    if cnt >= 3:
      tmp.remove(j)
      tmp.remove(j)
      tmp.remove(j)
      score = score + 1

  tmp_next = A[5:6]
  #print(tmp_next)
  cnt = tmp.count(tmp_next)
  #cnt = 0
  if cnt >= 1:
    tmp.remove(tmp_next)
    tmp = tmp + [tmp_next]
    A = tmp + A[5:]
  else:
    if len(tmp) == 2:
      A = tmp + A[5:]
      #print(A)
    else:
      A  =tmp[3:5] + A[5:]
      #print(A)

for k in range(0,N+1):
    cnt = A.count(k)
    #print(j,cnt)
    if cnt == 3:
      score = score + 1

print(score)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n = int(input())
A = list(map(int, input().split()))
remain = defaultdict(int)
mustflag = 0
must = set()

if n == 1:
    if A[0] == A[1] and A[1] == A[2]:
        print(1)
        exit()
    else:
        print(0)
        exit()
ans = 0
remain[A[0]] += 1
remain[A[1]] += 1
for i in range(1, n):
    nex = defaultdict(int)
    nex1 = A[3 * i - 1]
    nex2 = A[3 * i]
    nex3 = A[3 * i + 1]
    if nex1 == nex2 and nex2 == nex3:
        ans += 1
        continue
    elif nex1 == nex2:
        if remain[nex1] >= 1:
            remain[nex1] -= 1
            ans += 1
            mustflag = 1
            if mustflag:
                must.add(nex3)
            remain[nex3] += 1
        elif remain[nex3] >= 2:
            if mustflag:
                if nex3 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex3] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    must = set()
                    remain[nex1] += 2
                    if mustflag:
                        must.add(nex1)
            else:
                remain[nex3] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                must = set()
                remain[nex1] += 2
                if mustflag:
                    must.add(nex1)
        else:
            remain[nex1] += 2
            remain[nex3] += 1
            if mustflag:
                must.add(nex1)
                must.add(nex3)
    elif nex2 == nex3:
        if remain[nex2] >= 1:
            remain[nex2] -= 1
            ans += 1
            mustflag = 1
            if mustflag:
                must.add(nex1)
            remain[nex1] += 1
        elif remain[nex1] >= 2:
            if mustflag:
                if nex1 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex1] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    if mustflag:
                        must.add(nex2)
            else:
                remain[nex2] += 2
                remain[nex1] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                if mustflag:
                    must.add(nex2)
                remain[nex2] += 2
        else:
            remain[nex2] += 2
            remain[nex1] += 1
            if mustflag:
                must.add(nex2)
                must.add(nex1)
    elif nex3 == nex1:
        if remain[nex1] >= 1:
            remain[nex1] -= 1
            ans += 1
            mustflag = 1
            if mustflag:
                must.add(nex2)
            remain[nex2] += 1
        elif remain[nex2] >= 2:
            if mustflag:
                if nex2 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex2] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    must = set()
                    if mustflag:
                        must.add(nex1)
                    remain[nex1] += 2
            else:
                remain[nex2] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                must = set()
                if mustflag:
                    must.add(nex1)
                remain[nex1] += 2
        else:
            remain[nex1] += 2
            remain[nex2] += 1
            if mustflag:
                must.add(nex1)
                must.add(nex2)
    else:
        if remain[nex1] >= 2:
            if mustflag:
                if nex1 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex1] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    must = set()
                    remain[nex2] += 1
                    remain[nex3] += 1
            else:
                remain[nex1] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                must = set()
                remain[nex2] += 1
                remain[nex3] += 1
        elif remain[nex2] >= 2:
            if mustflag:
                if nex2 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex2] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    must = set()
                    remain[nex3] += 1
                    remain[nex1] += 1
            else:
                remain[nex2] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                must = set()
                remain[nex3] += 1
                remain[nex1] += 1
        elif remain[nex3] >= 2:
            if mustflag:
                if nex3 not in must:
                    remain[nex1] += 1
                    remain[nex2] += 1
                    remain[nex3] += 1
                    must.add(nex1)
                    must.add(nex2)
                    must.add(nex3)
                else:
                    remain[nex3] -= 2
                    ans += 1
                    remain = defaultdict(int)
                    mustflag = 0
                    must = set()
                    remain[nex1] += 1
                    remain[nex2] += 1
            else:
                remain[nex3] -= 2
                ans += 1
                remain = defaultdict(int)
                mustflag = 0
                must = set()
                remain[nex1] += 1
                remain[nex2] += 1
        else:
            remain[nex1] += 1
            remain[nex2] += 1
            remain[nex3] += 1
num = A[-1]
if mustflag:
    if num in must:
        ans += 1
else:
    if remain[A[-1]] >= 2:
        ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import *

N = int(input())
A = list(map(int, input().split()))
q = deque(A)
ans = 0

for _ in range(N-1):
    l = []
    
    for _ in range(5):
        l.append(q.popleft())
    
    l.sort()
    
    if l[0]==l[1]==l[2]:
        ans += 1
        q.appendleft(l[3])
        q.appendleft(l[4])
    elif l[2]==l[3]==l[4]:
        ans += 1
        q.appendleft(l[0])
        q.appendleft(l[1])
    else:
        cnt = defaultdict(int)
        
        for qi in q:
            cnt[qi] += 1
        
        l.sort(key=lambda x: cnt[x])
        q.appendleft(l[3])
        q.appendleft(l[4])

if q[0]==q[1]==q[2]:
    ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))

two = [0]*n
one = [0]*n

two[A[0]] += 1
two[A[1]] += 1

ans = 0
for i in range(n-1):
    nxt = A[3*i+2:3*i+5]
    nxt.sort()
    if nxt[0] == nxt[1] == nxt[2]:
        ans += 1
        continue
    if nxt[1] == nxt[2]:
        nxt.reverse()
    if nxt[0] == nxt[1]:
        if two[nxt[0]] >= 1:
            ans += 1
            two[nxt[0]] -= 1
            for i in range(n):
                one[i] += two[i]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if one[nxt[0]] >= 1:
            ans += 1
            one = two[:]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
            ans += 1
            two = [0]*n
            one = [0]*n
        else:
            two[nxt[2]] += 1
        two[nxt[0]] += 2
        continue
    if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[0]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[0]] += 1
        continue
    for i in range(3):
        two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
A = [int(i) for i in input().split()]

ans = 0
stack = 0
count = np.zeros(n)
for i in range(3*n):
  stack += 1
  count[A[i]-1] += 1
  if count[A[i]-1] == 3 and stack < 6:
    ans += 1
    count[A[i]-1] = 0 
    stack -= 3
  elif count[A[i]-1] == 3 and stack > 5:
    count = np.zeros(n)
    ans += 1
    stack -= 3
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))

two = [0]*n
one = [0]*n

two[A[0]] += 1
two[A[1]] += 1

ans = 0
for i in range(n-1):
    nxt = A[3*i+2:3*i+5]
    nxt.sort()
    if nxt[0] == nxt[1] == nxt[2]:
        ans += 1
        continue
    if nxt[1] == nxt[2]:
        nxt.reverse()
    if nxt[0] == nxt[1]:
        if one[nxt[0]] >= 1:
            ans += 1
            one = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[0]] >= 1:
            ans += 1
            two[nxt[0]] -= 1
            one = [0]*n
            for i in range(n):
                one[i] += two[i]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
            ans += 1
            two = [0]*n
            one = [0]*n
        else:
            two[nxt[2]] += 1
        two[nxt[0]] += 2
        continue
    if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[0]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[0]] += 1
        continue
    for i in range(3):
        two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

cnt = 1
if Counter(A[:5]).most_common()[0][1] == 3:
    cnt += 1
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:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    dp = defaultdict(int)
    dp[(A[0], A[1])] = 0

    for i in range(n - 1):
        ndp = defaultdict(int)
        k = max(dp.values())
        for key, val in dp.items():
            if val != k:
                continue
            B = list(key)
            for j in range(3 * i + 2, 3 * i + 5):
                B.append(A[j])
            B.sort()
            for s in range(5):
                for t in range(s + 1, 5):
                    C = B[:]
                    C.remove(B[s])
                    C.remove(B[t])
                    if len(set(C)) == 1:
                        ndp[B[s], B[t]] = max(ndp[B[s], B[t]], val + 1)
                    else:
                        ndp[B[s], B[t]] = max(ndp[B[s], B[t]], val)
        dp = ndp

    ans = 0
    for key, val in dp.items():
        if key[0] == key[-1] == A[-1]:
            val += 1
        ans = max(ans, val)
    print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
re=0
i=1
while i<=N:
  
  re=re+A.count(i)//3
  i+=1
print(re)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))

two = [0]*n
one = [0]*n

two[A[0]] += 1
two[A[1]] += 1

ans = 0
for i in range(n-1):
    nxt = A[3*i+2:3*i+5]
    nxt.sort()
    if nxt[0] == nxt[1] == nxt[2]:
        ans += 1
        continue
    if nxt[1] == nxt[2]:
        nxt.reverse()
    if nxt[0] == nxt[1]:
        if one[nxt[0]] >= 1:
            ans += 1
            one = two[:]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[0]] >= 1:
            ans += 1
            two[nxt[0]] -= 1
            for i in range(n):
                one[i] += two[i]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
            ans += 1
            two = [0]*n
            one = [0]*n
        else:
            two[nxt[2]] += 1
        two[nxt[0]] += 2
        continue
    if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[0]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[0]] += 1
        continue
    for i in range(3):
        two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    judge = [[0]*n for _ in range(n)]
    count = [0]*n
    
    for i in range(n*3):
        k = a[i]-1
        count[k] += 1
        if count[k] == 3:
            judge[k][max(0, (i-2)//3)] -= 2
        else:
            judge[k][max(0, (i-2)//3)] += 1
    
    for i in range(n):
        for j in range(n-1):
            judge[i][j+1] += judge[i][j]
    
    ans = 0
    for i in range(n):
        sub = 0
        for j in range(n):
            sub += judge[j][i]
        ans = max(ans, sub)
    
    ans = n-(ans-1)//2
    print(ans)
            
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split()))).reshape(1, -1)

score = 0
for i in range(n - 1):
    # sort
    a[0, 3 * i: 3 * i + 5].sort()
    if a[0, 3 * i] == a[0, 3 * i + 1] and a[0, 3 * i + 1] == a[0, 3 * i + 2]:
        # print(a[0, 3 * i: 3 * i + 3])
        score += 1
    elif a[0, 3 * i + 2] == a[0, 3 * i + 3] and a[0, 3 * i + 3] == a[0, 3 * i + 4]:
        # print(a[0, 3 * i + 2: 3 * i + 5])
        a[0, 3 * i: 3 * i + 5] = a[0, 3 * i: 3 * i + 5][::-1]
        score += 1
    # print(a)
if a[0, 3 * n - 3] == a[0, 3 * n - 2] and a[0, 3 * n - 2] == a[0, 3 * n - 1]:
    # print(a[0, 3 * n - 3: 3 * n])
    score += 1
# print(a)
# print()
print(score)
# print(a)
# a[0: 5].sort()
# print(a)
# print(np.sort(a[0: 5]))

Submission status: Wrong Answer

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


def main():
    import sys
    from collections import defaultdict
    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] * (N+1) for _ in range(N+1)]
    ma = [-N] * (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 = defaultdict(int)
        # 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)
            Q[ab] = max(Q[ab], val)
            Q[ba] = max(Q[ba], 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)
            Q[bc] = max(Q[bc], val)
            Q[cb] = max(Q[cb], 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)
            Q[ac] = max(Q[ac], val)
            Q[ca] = max(Q[ca], 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)
            Q[aj] = max(Q[aj], val)
            Q[ja] = max(Q[ja], 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)
            Q[bj] = max(Q[bj], val)
            Q[jb] = max(Q[jb], 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)
            Q[cj] = max(Q[cj], val)
            Q[jc] = max(Q[jc], 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:
n = int(input())
A = list(map(lambda x:int(x)-1,input().split()))

two = [0]*n
one = [0]*n

two[A[0]] += 1
two[A[1]] += 1

ans = 0
for i in range(n-1):
    nxt = A[3*i+2:3*i+5]
    nxt.sort()
    if nxt[0] == nxt[1] == nxt[2]:
        ans += 1
        continue
    if nxt[1] == nxt[2]:
        nxt.reverse()
    if nxt[0] == nxt[1]:
        if one[nxt[0]] >= 1:
            ans += 1
            one = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[0]] >= 1:
            ans += 1
            two[nxt[0]] -= 1
            for i in range(n):
                one[i] += two[i]
            two = [0]*n
            two[nxt[2]] += 1
            continue
        if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
            ans += 1
            two = [0]*n
            one = [0]*n
        else:
            two[nxt[2]] += 1
        two[nxt[0]] += 2
        continue
    if two[nxt[0]] >= 1 and two[nxt[0]] + one[nxt[0]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[1]] >= 1 and two[nxt[1]] + one[nxt[1]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[0]] += 1
        two[nxt[2]] += 1
        continue
    if two[nxt[2]] >= 1 and two[nxt[2]] + one[nxt[2]] >= 2:
        ans += 1
        two = [0]*n
        one = [0]*n
        two[nxt[1]] += 1
        two[nxt[0]] += 1
        continue
    for i in range(3):
        two[nxt[i]] += 1
if two[A[-1]] >= 1 and one[A[-1]] + two[A[-1]]>= 2:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
cards = list(map(int,input().split()))
five_cards = []
start_index = 0
end_index = 5
if N ==1:
    five_cards = cards
points = 0
for _ in range(N-1):
        five_cards = cards[start_index:end_index]
        five_cards = sorted(five_cards,key=five_cards.count,reverse=True)
        if five_cards.pop(0) == five_cards.pop(0) == five_cards.pop(0):
            points += 1
        start_index += 3
        end_index += 3
if len(set(five_cards))==1:
        points += 1
print(points)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

n = int(input())
A = list(map(int, input().split()))
remain = defaultdict(int)
must = defaultdict(int)

if n == 1:
    if A[0] == A[1] and A[1] == A[2]:
        print(1)
        exit()
    else:
        print(0)
        exit()
ans = 0
remain[A[0]] += 1
remain[A[1]] += 1
for i in range(1, n):
    nex = defaultdict(int)
    nex1 = A[3 * i - 1]
    nex2 = A[3 * i]
    nex3 = A[3 * i + 1]
    if nex1 == nex2 and nex2 == nex3:
        ans += 1
        continue
    elif nex1 == nex2:
        if remain[nex1] >= 1:
            remain[nex1] -= 1
            ans += 1
            remain[nex3] += 1
        elif remain[nex3] >= 2:
            remain[nex3] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex1] += 2
        else:
            remain[nex1] += 2
            remain[nex3] += 1
    elif nex2 == nex3:
        if remain[nex2] >= 1:
            remain[nex2] -= 1
            ans += 1
            remain[nex1] += 1
        elif remain[nex1] >= 2:
            remain[nex1] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex2] += 2
        else:
            remain[nex2] += 2
            remain[nex1] += 1
    elif nex3 == nex1:
        if remain[nex1] >= 1:
            remain[nex1] -= 1
            ans += 1
            remain[nex2] += 1
        elif remain[nex2] >= 2:
            remain[nex2] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex1] += 2
        else:
            remain[nex1] += 2
            remain[nex2] += 1
    else:
        if remain[nex1] >= 2:
            remain[nex1] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex2] += 1
            remain[nex3] += 1
        elif remain[nex2] >= 2:
            remain[nex2] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex3] += 1
            remain[nex1] += 1
        elif remain[nex3] >= 2:
            remain[nex3] -= 2
            ans += 1
            remain = defaultdict(int)
            remain[nex1] += 1
            remain[nex2] += 1
        else:
            remain[nex1] += 1
            remain[nex2] += 1
            remain[nex3] += 1

if remain[A[-1]] >= 2:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    A.reverse()
    ans = 0
    while A:
        if len(A) >= 5:
            c = Counter(A[-5:])
            for _ in range(5):
                A.pop()
            for k, v in reversed(c.most_common()):
                for _ in range(v):
                    A.append(k)
        b = set()
        for _ in range(3):
            b.add(A.pop())
        if len(b) == 1:
            ans += 1

    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = [0 for i in range(n + 1)]

ans = 0
cnt[a[0]] += 1
cnt[a[1]] += 1
i = 2

while i < 3 * n - 1:
  flag = False
  for j in range(3):
    cnt[a[i]] += 1
    if cnt[a[i]] == 3 and not flag:
      cnt[a[i]] -= 3
      flag = True
    i += 1
  if flag:
    ans += 1

cnt[a[3 * n - 1]] += 1
if cnt[a[3 * n - 1]] == 3 and ans == n - 1:
  ans += 1

print(ans)

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()

f0 = [0] * (n+1)
f1 = [0] * (n+1)
f2 = [0] * (n+1)
ans = 0
for i in range(3*n):
    f0[a[i]] += 1
    if i % 3 == 1:
        for j in range(n+1):
            if f0[j] >= 3:
                ans += 1
                f0[j] -= 3
                break
            if f0[j] + min(f1[j], 2) >= 3:
                ans += 1
                if f0[j] == 2:
                    f0[j] = 0
                    f1[j] -= 1
                    for k in range(n+1):
                        f2[k] += f1[k]
                        f1[k] = 0
                else:
                    f0[j] = 0
                    for k in range(n+1):
                        f2[k] = 0
                        f1[k] = 0
                break
            if f0[j] + min(f1[j], 1) + min(f2[j], 1) >= 3:
                ans += 1
                f0[j] -= 1
                for k in range(n+1):
                    f2[k] = 0
                    f1[k] = 0
                break
        for k in range(n+1):
            f1[k] += f0[k]
            f0[k] = 0
    # print(f0, f1, f2)

for j in range(n + 1):
    if f0[j] + min(f1[j], 2) >= 3 or f0[j] + min(f1[j], 1) + min(f2[j], 1) >= 3:
        ans += 1
print(ans)