atcoder#ABC176F. [ABC176F] Brave CHAIN
[ABC176F] Brave CHAIN
Score : points
Problem Statement
We have cards arranged in a row from left to right, where each card has an integer between and (inclusive) written on it. The integer written on the -th card from the left is .
You will do the following operation 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 point.
After these operations, if the integers written on the remaining three cards are all equal, you will gain additional point.
Find the maximum number of points you can gain.
Constraints
Input
Input is given from Standard Input in the following format:
Output
Print the maximum number of points you can gain.
2
1 2 1 2 2 1
2
Let us rearrange the five leftmost cards so that the integers written on the six cards will be from left to right.
Then, remove the three leftmost cards, all of which have the same integer , gaining point.
Now, the integers written on the remaining cards are .
Since these three cards have the same integer , we gain more point.
In this way, we can gain points - which is the maximum possible.
3
1 1 2 2 3 3 3 2 1
1
3
1 1 2 2 2 3 3 3 1
3