AtCoder Beginner Contest 176
F
Brave CHAIN
AtCoder
⌀ 2912
原题
← E→
翻译中…

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.

翻译中…

样例

Input #1
2
1 2 1 2 2 1
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.
Input #2
3
1 1 2 2 3 3 3 2 1
Output #2
1
Input #3
3
1 1 2 2 2 3 3 3 1
Output #3
3
题目信息
题号
abc176_f
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。