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

#BS0000207. Truy vết dãy con tăng dài nhất (What Goes Up)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

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 a1,a2,…,ana_1,a_2,\ldots,a_n is given. A subsequence selects positions i1<i2<⋯<iki_1<i_2<\cdots<i_k without requiring them to be consecutive. It is strictly increasing when ai1<ai2<⋯<aika_{i_1}<a_{i_2}<\cdots<a_{i_k}. 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 nn. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in their original order.

Output

Let LL be the maximum length. Print LL on the first line and a single hyphen - on the second. Print the selected subsequence in order, one value per line, on the next LL lines.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 (1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 (1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 (1≤i≤n1\le i\le n).

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 1,2,2,2,3,4,4,41,2,2,2,3,4,4,4. 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 (−7-7). This gives −7,2,3,6-7,2,3,6.