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

#BS0000206. Độ dài dãy con tăng dài nhất (Longest Increasing Subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Độ dài dãy con tăng dài nhất (Longest Increasing Subsequence)

Longest Increasing Subsequence

Source: LeetCode

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 the maximum length of such a subsequence.

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

Print one integer: the maximum length.

Subtasks

  • Subtask 1 (20 points): 1≤n≤151\le n\le 15; −10000≤ai≤10000-10000\le a_i\le 10000 (1≤i≤n1\le i\le n).
  • Subtask 2 (30 points): 1≤n≤2001\le n\le 200; −10000≤ai≤10000-10000\le a_i\le 10000 (1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤25001\le n\le 2500; −10000≤ai≤10000-10000\le a_i\le 10000 (1≤i≤n1\le i\le n).

Examples

Input

8
10 9 2 5 3 7 101 18

Output

4

Explanation

There are eight numbers. Positions 3, 5, 6 and 8 give 2,3,7,182,3,7,18, of length 4. The maximum lengths ending at the eight positions are 1,1,1,2,2,3,4,41,1,1,2,2,3,4,4. For instance, 7 can follow 2 and 3, and 18 can follow 7 but not 101. No position ends a longer subsequence.