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:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

dp=[[-INF]*N for _ in range(N)]
dp[max(A[0],A[1])][min(A[0],A[1])]=0
A=A[2:]

ans=0
MAX=0
MAX_one=[0]*N
for i in range(N-1):
    tmp=A[i*3:i*3+3]
    tmp.sort()
    if tmp[0]==tmp[1]==tmp[2]:
        ans+=1
    else:
        if tmp[0]==tmp[1] or tmp[1]==tmp[2]:
            if tmp[0]==tmp[1]:
                rem=tmp[2]
            else:
                rem=tmp[0]
            for j in range(N):
                dp[max(j,rem)][min(j,rem)]=max(dp[max(j,rem)][min(j,rem)],dp[max(tmp[1],j)][min(tmp[1],j)]+1)
                MAX=max(MAX,dp[max(j,rem)][min(j,rem)])
                MAX_one[j]=max(MAX_one[j],dp[max(j,rem)][min(j,rem)])
                MAX_one[rem]=max(MAX_one[rem],dp[max(j,rem)][min(j,rem)])
        else:
            for k in range(3):
                dp[tmp[(k+1)%3]][tmp[(k+2)%3]]=max(dp[tmp[(k+1)%3]][tmp[(k+2)%3]],dp[tmp[k]][tmp[k]]+1)
                MAX=max(MAX,dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                MAX_one[tmp[(k+1)%3]]=max(MAX_one[tmp[(k+1)%3]],dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                MAX_one[tmp[(k+2)%3]]=max(MAX_one[tmp[(k+2)%3]],dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                
        for k in range(3):
            dp[max(tmp[k],tmp[(k+1)%3])][min(tmp[k],tmp[(k+1)%3])]=MAX
            MAX_one[tmp[k]]=max(MAX_one[tmp[k]],MAX)
            MAX_one[tmp[(k+1)%3]]=max(MAX_one[tmp[(k+1)%3]],MAX)
            for j in range(N):
                dp[max(j,tmp[k])][min(j,tmp[k])]=max(dp[max(j,tmp[k])][min(j,tmp[k])],MAX_one[j])
                MAX_one[j]=max(MAX_one[tmp[k]],MAX_one[j])
                MAX_one[tmp[k]]=max(MAX_one[tmp[k]],MAX_one[j])
ans+=max(MAX,dp[A[-1]][A[-1]]+1)
print(ans)

Submission status: Wrong Answer

Solution code:

##main#######################################

## INPUT ####################################
N=int(input())
#A = list(map(int,list(input().split())))
A = input()
AA= A.replace(" ","")

## PROCESSING ###############################
ans=0

#for idx in range(N*3):
#print("first:"+ AA)
while len(AA)>0:
	#print(AA)
	AAA=AA[0:5]
	AAAA= list(AAA)
	AAAA.sort()

	
	c=0
	i=0
	deletieNumFlg=0
	while i < len(AAAA):
		CHAR1=AAAA[i]
		c=AAA.count(CHAR1)

		#print("dddd1")
		#print(CHAR1)
		#print("c:" + str( c))
		#print("dddd2")


		if c>=3:
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			ans=ans+1
			deletieNumFlg=1
			i=99
		else :
			i=i+c
			deletieNumFlg=0

	
	#print("flg:" + str(deletieNumFlg))
	#print("next(bef):" + AA)
	if deletieNumFlg==1:
		AA = ''.join(AAAA) + AA[5:]
	else:
		AA = AA[3:]

	#print("next(aft):" + AA)

## OUTPUT ###################################

#print("++++OUTPUT++++")
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], A[-6:].count(key), key))
            lis = sorted(lis)
            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:

##main#######################################

## INPUT ####################################
N=int(input())
#A = list(map(int,list(input().split())))
A = input()
AA= A.replace(" ","")

## PROCESSING ###############################
ans=0

#for idx in range(N*3):
#print("first:"+ AA)
while len(AA)>0:
	#print(AA)
	AAA=AA[0:5]
	AAAA= list(AAA)
	AAAA.sort()

	
	c=0
	i=0
	deletieNumFlg=0
	while i < len(AAAA):
		CHAR1=AAAA[i]
		c=AAA.count(CHAR1)

		#print("dddd1")
		#print(CHAR1)
		#print("c:" + str( c))
		#print("dddd2")


		if c>=3:
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			ans=ans+1
			deletieNumFlg=1
			i=99
		else :
			i=i+c

	
	#print("flg:" + str(deletieNumFlg))
	#print("next(bef):" + AA)
	if deletieNumFlg==1:
		AA = ''.join(AAAA) + AA[5:]
	else:
		AA = AA[3:]

	#print("next(aft):" + AA)

## OUTPUT ###################################

#print("++++OUTPUT++++")
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], A[-6:].count(key), key))
            lis = sorted(lis, key = lambda x:(x[0], x[1]))
            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(""))
card = input("").split()
score = 0
for i in range(n-1):
    new_list = card[0:5]
    kekka = "nasi"
    count = []
    for j in range(1, n+1):
        count.append(new_list.count(str(j)))
        if new_list.count(str(j)) >= 3:
            score += 1
            kekka = "ari"
            for num in range(3):
                index = card.index(str(j))
                del card[index]
    if kekka == "nasi":
        del card[0:3]      
            
for i in range(1, n+1):
    if card.count(str(i)) >= 3:
        score += 1
print(score)

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], A[-6:].count(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=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:
  d[mi]=0
  ans+=1

for i in range(1,n):
  d[a[(i+1)*3-1]]+=1
  if i<n-1:
    d[a[(i+1)*3]]+=1
    d[a[(i+1)*3+1]]+=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[a[(i+1)*3-1]]=1
    if i<n-1:
      d[a[(i+1)*3]]=1
      d[a[(i+1)*3+1]]=1
      d[mi]=0
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict as dd
N = int(input())
As = list(map(int, input().split()))

cts = [0] * (N+1)
idxs = dd(lambda: dd(int))

arr = []

for i, a in enumerate(As):
    c = cts[a]
    idxs[a][c] = i
    arr.append((a, c))
    cts[a] += 1

arr.reverse()
ans = 0
for _ in range(N-1):
    cards = [arr.pop() for _ in range(5)]
    cards.reverse()
    ct = Counter([c[0] for c in cards])
    mcn, mcc = ct.most_common()[0]
    if mcc >= 3:
        ans += 1
        n = 0
        for c in cards:
            if c[0] == mcn:
                n += 1
            if n > 3 or c[0] != mcn:
                arr.append(c)
    else:
        # 1枚目がすでに捨てられていたら、捨てて良い
        firsts = []
        for c in cards:
            if c[1]+3 <= cts[c[0]]:
                firsts.append(c)
        # 3枚目が一番近いのを残す
        firsts.sort(key=lambda x: idxs[x[0]][x[1]+2])
        n = 0
        for f in firsts:
            for c in cards:
                if f[0] == c[0]:
                    arr.append(c)
                    n += 1
                    if n >= 2: break
            if n >= 2: break
        while n < 2:
            arr.append((0, 2))
            n += 1

if arr[0][0] == arr[1][0] == arr[2][0]:
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input(""))
card = input("").split()
score = 0
for i in range(n-1):
    new_list = card[0:5]
    print(new_list)
    kekka = "nasi"
    count = []
    for j in range(1, n+1):
        count.append(new_list.count(str(j)))
        if new_list.count(str(j)) >= 3:
            score += 1
            kekka = "ari"
            for num in range(3):
                index = card.index(str(j))
                del card[index]
    if kekka == "nasi":
        del card[0:3]      
            
for i in range(1, n+1):
    if card.count(str(i)) >= 3:
        score += 1
print(score)

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], A[-10:].count(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:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

dp=[[-INF]*N for _ in range(N)]
dp[max(A[0],A[1])][min(A[0],A[1])]=0
A=A[2:]

ans=0
MAX=0
MAX_one=[0]*N
for i in range(N-1):
    tmp=A[i*3:i*3+3]
    tmp.sort()
    if tmp[0]==tmp[1]==tmp[2]:
        ans+=1
    else:
        if tmp[0]==tmp[1] or tmp[1]==tmp[2]:
            if tmp[0]==tmp[1]:
                rem=tmp[2]
            else:
                rem=tmp[0]
            for j in range(N):
                dp[max(j,rem)][min(j,rem)]=max(dp[max(j,rem)][min(j,rem)],dp[max(tmp[1],j)][min(tmp[1],j)]+1)
                MAX=max(MAX,dp[max(j,rem)][min(j,rem)])
                MAX_one[j]=max(MAX_one[j],dp[max(j,rem)][min(j,rem)])
                MAX_one[rem]=max(MAX_one[rem],dp[max(j,rem)][min(j,rem)])
        else:
            for k in range(3):
                dp[tmp[(k+1)%3]][tmp[(k+2)%3]]=max(dp[tmp[(k+1)%3]][tmp[(k+2)%3]],dp[tmp[k]][tmp[k]]+1)
                MAX=max(MAX,dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                MAX_one[tmp[(k+1)%3]]=max(MAX_one[tmp[(k+1)%3]],dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                MAX_one[tmp[(k+2)%3]]=max(MAX_one[tmp[(k+2)%3]],dp[tmp[(k+1)%3]][tmp[(k+2)%3]])
                
                dp[max(tmp[k],tmp[(k+1)%3])][min(tmp[k],tmp[(k+1)%3])]=MAX
                MAX_one[tmp[k]]=max(MAX_one[tmp[k]],MAX)
                MAX_one[tmp[(k+1)%3]]=max(MAX_one[tmp[(k+1)%3]],MAX)
            
                for j in range(N):
                    dp[max(j,tmp[k])][min(j,tmp[k])]=max(dp[max(j,tmp[k])][min(j,tmp[k])],MAX_one[j])
                    MAX_one[tmp[k]]=max(MAX_one[tmp[k]],MAX_one[j])
ans+=max(MAX,dp[A[-1]][A[-1]]+1)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = [0] * N
last = [0] * N
used = [0] * N

if A == [1, 1, 1]:
  print(1)
  quit()

for i in range(3 * N):
  now = A[i] - 1
  count[now] += 1
  if count[now] == 3:
    last[now] = i
  #print(now)  
#print(count, last)    

score = 0
now = [A[0], A[1], A[2], A[3], A[4]]
for i in range(N - 1):
  L = [0] * 5
  for j in range(5):
    L[j] = [last[now[j] - 1], j, used[now[j] - 1]]
  L = sorted(L)
  #print(L)
  if L[1][0] == L[2][0]:
    if L[0][0] == L[1][0]:
      score += 1
      one, two = L[3][1], L[4][1]
    elif L[2][0] == L[3][0]:
      score += 1
      one, two = L[0][1], L[4][1]
    else:
      L = sorted(L, key = lambda x:x[2])
      one, two = L[0][1], L[1][1]
      used[L[2][2]] = 1
      used[L[3][2]] = 1
      used[L[4][2]] = 1
  elif (L[2][0] == L[3][0]) and (L[3][0] == L[4][0]):
    score += 1
    one, two = L[0][1], L[1][1]
  else:
    L = sorted(L, key = lambda x:x[2])
    one, two = L[0][1], L[1][1]
    used[L[2][2]] = 1
    used[L[3][2]] = 1
    used[L[4][2]] = 1
    
  if i != N - 2:
    now = [A[one], A[two], A[5 + 3 * i], A[6 + 3 * i], A[7 + 3 * i]]
  else:
    now = [A[one], A[two], A[-1]]
  
if now[0] == now[1] and now[1] == now[2]:
  score += 1
    
print(score)

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], A[-5:].count(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:
a = [input().split() for l in range(2)]
c = a[1]
N = int(a[0][0])
p = 0

for i in range(N):
    suff = c[:5]
    for j in range(N):
        if suff.count(j) == 5:
            p = p + 1
            suff.remove(j)
            suff.append(j)
            suff.append(j)
        elif suff.count(j) == 4:
            p = p + 1
            suff.remove(j)
            suff.append(j)
        elif suff.count(j) == 3:
            p = p + 1
            suff.remove(j)
    s = c[:8]
    for k in range(N):
        if s.count(k) > 2:
            p = p + 1
        i = i + 1
        del s[:6]
    c[:5] = suff
print(p)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
A = [A[i]-1 for i in range(3*n)]
have1 = [0 for _ in range(n)]
have2 = [0 for _ in range(n)]
have2[A[0]]+=1;have2[A[1]]+=1

answer = 0
for i in range(n-1):
    a1,a2,a3 = A[3*i+2],A[3*i+3],A[3*i+4]
    have2[a1]+=1;have2[a2]+=1;have2[a3]+=1
    if have1[a1] + have2[a1]>=3:
        answer +=1
        have2[a1] = have1[a1] + have2[a1] -3
        have1 = have2
        have2 = [0 for _ in range(n)]
    if have1[a2]+have2[a2]>=3:
        answer +=1
        have2[a2] = have1[a2]+have2[a2]-3
        have1 = have2
        have2 = [0 for _ in range(n)]
    if have1[a3]+ have2[a3]>=3:
        answer +=1
        have2[a3] = have1[a3]+ have2[a3]-3
        have1 = have2
        have2 = [0 for _ in range(n)]
if have1[A[-1]] + have2[A[-1]] +1 >=3:
    answer +=1
print(answer)

Submission status: Wrong Answer

Solution code:
print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
print(n)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
a_list_sort = sorted(a_list)
# print(a_list_sort)
count = 0
for i in range(n):
    start = i*3
    a = a_list_sort[start:start+3]
    # print(a)
    if len(set(a)) == 1:
        count += 1

print(count)

Submission status: Wrong Answer

Solution code:
# A-C 簡単に感じた
# D　時間制限がpyにはキツかった。pyは競プロ不適切、というメッセージか、あるいは改善の予知を見出せという教訓なのだろうか...(後者ですね、頑張りましょう)
#E 鳩の巣論法的な何か。

Submission status: Wrong Answer

Solution code:

max2 = lambda x,y: x if x > y else y
min2 = lambda x,y: x if x < y else y

def solve(A):
    N = len(A)//3
    encode = lambda x,y: min2(x*N+y,y*N+x)
    decode = lambda i: (i//N,i%N)

    dp = [-1000]*(N*N)
    offset = 0
    t = iter(A)
    a,b = next(t),next(t)
    dp[encode(a,b)] = 0

    for a,b,c in zip(t,t,t):
        if a == b == c:
            offset += 1
        elif a == b or b == c or c == a:
            x = a if (a==b or a == c) else b
            y = a if b == c else (b if c == a else c)

            temp = dp[encode(y,y)]

            for i in range(N):
                p = encode(i,y)
                dp[p] = max2(dp[p],dp[encode(i,x)]+1)
            p = encode(x,x)
            dp[p] = max2(dp[p],temp+1)
        else:
            p = encode(b,c)
            dp[p] = max2(dp[p],dp[encode(a,a)]+1)
            p = encode(c,a)
            dp[p] = max2(dp[p],dp[encode(b,b)]+1)
            p = encode(a,b)
            dp[p] = max2(dp[p],dp[encode(c,c)]+1)

    a = A[-1]
    r = max(dp)
    r = max2(r,dp[encode(a,a)]+1)+offset
    return r









if __name__ == '__main__':
    N = int(input())
    A = list(map(lambda x:int(x)-1,input().split()))
    print(solve(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = [0] * N
last = [0] * N
used = [0] * N

if A == [1, 1, 1]:
  print(1)
  quit()

for i in range(3 * N):
  now = A[i] - 1
  count[now] += 1
  if count[now] == 3:
    last[now] = i
  #print(now)  
#print(count, last)    

score = 0
now = [A[0], A[1], A[2], A[3], A[4]]
for i in range(N - 1):
  L = [0] * 5
  for j in range(5):
    L[j] = [last[now[j] - 1], j]
  L = sorted(L)
  #print(L)
  if L[1][0] == L[2][0]:
    if L[0][0] == L[1][0]:
      score += 1
      one, two = L[3][1], L[4][1]
    elif L[2][0] == L[3][0]:
      score += 1
      one, two = L[0][1], L[4][1]
    else:
      one, two = L[0][1], L[1][1]
  elif (L[2][0] == L[3][0]) and (L[3][0] == L[4][0]):
    score += 1
    one, two = L[0][1], L[1][1]
  else:
    one, two = L[0][1], L[1][1]
    
  if i != N - 2:
    now = [A[one], A[two], A[5 + 3 * i], A[6 + 3 * i], A[7 + 3 * i]]
  else:
    now = [A[one], A[two], A[-1]]
  
if now[0] == now[1] and now[1] == now[2]:
  score += 1
    
print(score)

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 == 1:
    print(z * (n-1))
elif z < n:
    print(z * n)
else:
    print(z)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

ima = [0 for i in range(n)]
for i in range(3*n):
    a[i] -= 1

count = 0
data = a[0:5]
data.sort()
po = s(data)
for ab in po:
    if ab[1] >= 3:
        ima[ab[0]] += ab[1]-3
        count += 1
    else:
        ima[ab[0]] += ab[1]

for i in range(n-2):
    data = a[5+2*i:5+2*i+3]
    data.sort(reverse = True)
    po = s(data)
    for ab in po:
        if ab[1] + ima[ab[0]] >= 3:
            use = 3 - ab[1]
            count += 1
            if use != 0:
                for j in range(n):
                    ima[j] = min(ima[j],2-use)
            break
    else:
        for ab in po:
            ima[ab[0]] += ab[1]
            ima[ab[0]] = min(2,ima[ab[0]])
if ima[a[-1]] == 2:
    count += 1
print(count)

Submission status: Wrong Answer

Solution code:

##main#######################################

## INPUT ####################################
N=int(input())
#A = list(map(int,list(input().split())))
A = input()
AA= A.replace(" ","")

## PROCESSING ###############################
ans=0

#for idx in range(N*3):
while len(AA)>0:
	#print(AA)
	AAA=AA[0:5]
	AAAA= list(AAA)
	AAAA.sort()

	
	c=0
	i=0
	deletieNumFlg=0
	while i < len(AAAA):
		CHAR1=AAAA[i]
		c=AAA.count(CHAR1)

		#print("dddd1")
		#print(CHAR1)
		#print("c:" + str( c))
		#print("dddd2")


		if c>=3:
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			AAAA.remove(CHAR1)
			ans=ans+1
			deletieNumFlg=1
			i=99
		else :
			i=i+c

	
	#print("flg:" + str(deletieNumFlg))
	if deletieNumFlg==1:
		AA = ''.join(AAAA) + AA[5:]
	else:
		AA = AA[3:]

	#print("next:" + AA)

## OUTPUT ###################################

#print("++++OUTPUT++++")
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

ima = [0 for i in range(n)]
for i in range(3*n):
    a[i] -= 1

count = 0
data = a[0:5]
data.sort()
po = s(data)
for ab in po:
    if ab[1] >= 3:
        ima[ab[0]] += ab[1]-3
        count += 1
    else:
        ima[ab[0]] += ab[1]

for i in range(n-2):
    data = a[5+2*i:5+2*i+3]
    data.sort()
    po = s(data)
    for ab in po:
        if ab[1] + ima[ab[0]] >= 3:
            use = 3 - ab[1]
            count += 1
            if use != 0:
                for j in range(n):
                    ima[j] = min(ima[j],2-use)
            break
    else:
        for ab in po:
            ima[ab[0]] += ab[1]
            ima[ab[0]] = min(2,ima[ab[0]])
if ima[a[-1]] == 2:
    count += 1
print(count)

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:
  d[mi]=0
  ans+=1

for i in range(1,n):
  d[a[(i+1)*3-1]]+=1
  if i<n-1:
    d[a[(i+1)*3]]+=1
    d[a[(i+1)*3+2]]+=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[a[(i+1)*3-1]]+=1
    if i<n-1:
      d[a[(i+1)*3]]+=1
      d[a[(i+1)*3+1]]+=1
      d[mi]-=1
print(ans)