The Score Chart
InterviewTime limit1sMemory limit128 MB
Given a sequence of 0s, 1s, and 2s, find the length of the longest subsequence that never decreases.
- Level
Easy3 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
The ghost Adi trains hard for the programming contest Ghostcoder. Every contest has exactly problems to solve, so a contest result is the number of solved problems, which is one of , , .
Adi carefully writes down the results of the contests in order. Now he wants to show off his growth to his friends. That is, he wants to pick some of the recorded results, keeping them in contest order, so that the chart drawn by the picked results never goes down (each picked result is not smaller than the one picked right before it), while making the number of picked results as large as possible.
Given the record of contest results, find the maximum length of such a chart that Adi can obtain.
Input
The first line contains the number of contests Adi took part in, ().
The second line contains integers, the results of Adi's contests in order, separated by spaces. Each result is one of , , .
Output
Print one integer: the length of the longest score chart Adi can obtain that never decreases. Here, the chart never decreasing means that, listing the picked results in contest order, each result is greater than or equal to the one right before it.