Solitaire
InterviewTime limit2sMemory limit1024 MB
Find the length of the longest subsequence of the cards that is strictly increasing and alternates between even and odd numbers.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Sometimes Rick remembers that he is no longer young and prefers to rest from endless adventures. One early evening he wanted to play solitaire, but he had no ordinary playing cards. After searching the house, Rick found cards with natural numbers written on them and decided to build a solitaire layout from them.
Since the cards were not made for solitaire, Rick invented his own rules. To make up for the lack of suits, he wants the cards to alternate so that adjacent cards have different remainders when divided by two. In the laid-out sequence the numbers must alternate as even, odd, even, and so on. Also, the number on each card must be strictly less than the number on the next card.
Rick shuffled the deck carefully and started playing. Morty, watching him, wondered what is the longest sequence of cards that satisfies Rick's conditions that can be taken from this deck. Help him find out.
Input
The first line contains an integer (), the number of cards.
The second line contains natural numbers (), the numbers written on the cards.
Output
Print one number, the length of the longest sequence that can be obtained from the given deck.
Hint
In the first sample, in the best case Rick draws the cards in the same order as given. The sequence satisfies the condition.
In the second sample, the sequence fits.