#BS0000207. Truy vết dãy con tăng dài nhất (What Goes Up)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Truy vết dãy con tăng dài nhất (What Goes Up)
What Goes Up
Source: UVa
Version: Phuoc Hung OJ Extended
Problem Statement
An integer sequence is given. A subsequence selects positions without requiring them to be consecutive. It is strictly increasing when . Find a longest strictly increasing subsequence. Among equal-length choices, prefer the one occurring later: compare the index sequences from right to left and choose the one with the larger index at the first difference. Thus the last index has priority, then the penultimate index, and so on. Print the values at the selected positions.
Input
The first line contains the sequence length . The second line contains integers in their original order.
Output
Let be the maximum length. Print on the first line and a single hyphen - on the second. Print the selected subsequence in order, one value per line, on the next lines.
Subtasks
- Subtask 1 (20 points): ; ().
- Subtask 2 (30 points): ; ().
- Subtask 3 (50 points): ; ().
Examples
Input
8
-7 10 9 2 3 8 8 6
Output
4
-
-7
2
3
6
Explanation
The eight positions have maximum ending lengths . The optimum is 4. Positions 6, 7 and 8 all achieve it, so choose position 8. Before its value 6, select position 5 (3), then position 4 (2), then position 1 (). This gives .