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

#FT0000101. Số dãy con tăng dài nhất (Number of Longest Increasing Subsequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Số dãy con tăng dài nhất (Number of Longest Increasing Subsequence)

Number of Longest Increasing Subsequence

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an integer array a1,a2,…,ana_1,a_2,\ldots,a_n. A subsequence is formed by selecting positions in increasing order, without requiring those positions to be consecutive. A subsequence is strictly increasing if each selected value is smaller than the following selected value.

Let LL be the maximum length of a strictly increasing subsequence. Count the number of ways to select a subsequence of length exactly LL.

Subsequences with different index sets are counted separately even if their selected values are identical. The answer is guaranteed to fit in a signed 32-bit integer.

Input

  • The first line contains integer nn, the array length.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in order.

Output

Print one integer: the number of strictly increasing subsequences of maximum length.

Subtasks

  • Subtask 1 (20 points): 1≤n≤141\le n\le 14; −106≤ai≤106-10^6\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; the answer fits in a signed 32-bit integer.
  • Subtask 2 (30 points): 1≤n≤2001\le n\le 200; −106≤ai≤106-10^6\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; the answer fits in a signed 32-bit integer.
  • Subtask 3 (50 points): 1≤n≤20001\le n\le 2000; −106≤ai≤106-10^6\le a_i\le 10^6 for every 1≤i≤n1\le i\le n; the answer fits in a signed 32-bit integer.

Examples

Example 1

Input

5
1 3 5 4 7

Output

2

Explanation

For (1,3,5,4,7)(1,3,5,4,7), the maximum increasing length is 44. Exactly two index sets achieve it: (1,2,3,5)(1,2,3,5) produces (1,3,5,7)(1,3,5,7), while (1,2,4,5)(1,2,4,5) produces (1,3,4,7)(1,3,4,7). Values 5,45,4 cannot both be consecutive in an increasing subsequence because 5>45>4. The output is 2.

Example 2

Input

5
2 2 2 2 2

Output

5

Explanation

All five values are 22, so no strictly increasing subsequence has length greater than 11. Each of the five positions forms a different length-11 subsequence. The output is 5.