#GD0000009. Dãy con tăng tham lam (Greedily Increasing Subsequence)
Dãy con tăng tham lam (Greedily Increasing Subsequence)
Greedily Increasing Subsequence
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Given a permutation of , build its greedily increasing subsequence (GIS): set , then repeatedly take the first later element that is strictly larger than the last chosen element. Stop when no such element remains. Print the GIS.
Input
The first line contains . The second line contains the permutation .
Output
Print the GIS length on the first line and the GIS elements on the second line.
Subtasks
General constraints:
-
.
-
is a permutation of .
-
Subtask 1 (20 points):
-
Subtask 2 (30 points):
-
Subtask 3 (50 points): No additional constraints.
Examples
Input
7
2 3 1 5 4 7 6
Output
4
2 3 5 7
Explanation
Start with . The first later value larger than is , then , then . No later value exceeds .