Family Addition
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Marichka and Zenyk thought about it for a long time, weighing all the pros and cons. There were many discussions, arguments, and misunderstandings. They were not sure if they were ready for such responsibility. But it seems they have finally decided on this step. There will finally be an addition to the family!
There was a very wide selection of cats in the pet store — for all tastes. No longer could anyone remain indifferent. Their eyes were wide with such variety. Marichka really wanted to choose that very single, best, happiest, kindest cat. Zenyk was simply happy that they would finally have a cat at home.
The selection process turned out to be quite complicated. The key parameter for Marichka turned out to be the fluffiness level of the cats.
All \(n\) cats in the pet store lined up in a row. The \(i\)-th cat has a fluffiness level equal to \(a_i\). The selection process was multi-stage. At each stage, Marichka rejected some cats, and they left the contest. The rest advanced to the next round without changing their relative order.
The rules were as follows: if directly to the right of some cat stood a cat with a higher fluffiness level, then at this stage, this cat was considered under-fluffy. At the end of each stage, all under-fluffy cats were eliminated from the contest. Note that first the under-fluffy cats were identified, and only then all of them were eliminated from the contest.
As a result of this entire process, a certain sequence of cats remained, such that none of them could be called under-fluffy by Marichka. The couple will take one of these lucky ones home.
Your task is to determine the resulting sequence of non-under-fluffy cats.
Input
The first line contains an integer \(n\) — the number of cats in the pet store.
The next line contains \(n\) integers \(a_i\) — the fluffiness levels of the cats in the order they lined up.
Output
In the first line, print an integer \(m\) — the number of cats that will remain.
In the second line, print \(m\) integers — the fluffiness levels of the cats.
Constraints
\(1 \le a_i \le 10^9\),
\(50\%\) of tests: \(1 \le n \le 1000\),
\(50\%\) of tests: \(1 \le n \le 10^5\),
it is guaranteed that all cats have distinct fluffiness levels.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 6 5 7 2 3 1 4 | 2 7 4 |
| Input (stdin) | Output (stdout) |
|---|---|
| 3 3 2 1 | 3 3 2 1 |
Notes
Let’s analyze the first example.
In the first stage, the under-fluffy cats will be the ones with fluffiness 5, 2, and 1. Therefore, the sequence becomes [6, 7, 3, 4]. In the second stage, the under-fluffy cats will be 6 and 3. Only cats 7 and 4 will remain, each of which is not under-fluffy.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|