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

#CT00050. Dãy tăng (Longest Increasing Subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy tăng (Longest Increasing Subsequence)

Longest Increasing Subsequence

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence AA of length NN, a1,a2,…,aNa_1,a_2,\ldots,a_N, a subsequence is obtained by selecting elements while preserving their order in the original sequence. It is strictly increasing if each subsequent element is greater than its predecessor.

Find the maximum possible length of a strictly increasing subsequence.

Input

The first line contains a positive integer NN.

The second line contains NN integers a1,a2,…,aNa_1,a_2,\ldots,a_N.

Output

Print one integer: the length of a longest strictly increasing subsequence.

Subtasks

∣ai∣≤109|a_i|\le10^9.

  • Subtask 1 (20 points): 1≤N≤201\le N\le20.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le3000.
  • Subtask 3 (50 points): 1≤N≤2⋅1051\le N\le2\cdot10^5.

Examples

Example 1

Input

8
3 1 2 1 8 5 6 7

Output

5

Explanation

Choose 11 at position 22, 22 at 33, 55 at 66, 66 at 77, and 77 at 88. The resulting subsequence (1,2,5,6,7)(1,2,5,6,7) has length five, which is maximal.

Example 2

Input

5
5 4 3 2 1

Output

1

Explanation

The sequence is strictly decreasing. No pair of elements selected in original order can increase, so any single element forms a longest valid subsequence of length one.

Example 3

Input

6
1 2 2 3 3 4

Output

4

Explanation

Choose 11, one of the two 22s, one of the two 33s, and 44. This gives length four. Equal values cannot be consecutive in a strictly increasing subsequence, so length five or six is impossible.