PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000208. Dãy con tăng nhỏ nhất theo thứ tự từ điển (Increasing Subsequence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Dãy con tăng nhỏ nhất theo thứ tự từ điển (Increasing Subsequence)

Increasing Subsequence

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

An independently specified Phuoc Hung OJ exercise inspired by Increasing Subsequence. The input/output format and value-based tie-breaking are defined for this version.

Given a sequence of nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, a subsequence is formed by selecting elements in their original order, not necessarily consecutively. It is strictly increasing if each element after the first is greater than its predecessor.

Find a strictly increasing subsequence of maximum length. Among all such subsequences, choose the one whose values are lexicographically smallest. For two distinct sequences of equal length, the sequence with the smaller value at their first differing position is smaller. Output the values, not their indices.

Input

The first line contains the integer nn.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

On one line, print the maximum length LL followed by the LL selected values, separated by spaces.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le12; −109≤ai≤109-10^9\le a_i\le10^9 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤2001\le n\le200; −109≤ai≤109-10^9\le a_i\le10^9 for every 1≤i≤n1\le i\le n; ai≤ai+1a_i\le a_{i+1} for every 1≤i<n1\le i<n.
  • Subtask 3 (50 points): 1≤n≤2001\le n\le200; −109≤ai≤109-10^9\le a_i\le10^9 for every 1≤i≤n1\le i\le n.

Examples

Input

6
3 1 2 1 2 4

Output

3 1 2 4

Explanation

The subsequence (1,2,4)(1,2,4) uses positions 2,3,62,3,6. There are only four distinct values, 1,2,3,41,2,3,4, but 33 appears before every occurrence of 11 and 22, so all four cannot be selected in increasing order. The maximum length is therefore 33. The displayed sequence attains that length, starts with the smallest possible value 11, and then takes the smallest possible next value 22. No length-three increasing subsequence is lexicographically smaller. Print 33 followed by 1,2,41,2,4.