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

#BS0000209. Dãy Wavio dài nhất (Wavio Sequence)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Dãy Wavio dài nhất (Wavio Sequence)

Wavio Sequence

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

An integer sequence a1,a2,…,ana_1,a_2,\ldots,a_n is given. A Wavio sequence has odd length 2k+12k+1 for k≥0k\ge0. Its values b1,…,b2k+1b_1,\ldots,b_{2k+1} satisfy b1<⋯<bk+1b_1<\cdots<b_{k+1} and bk+1>⋯>b2k+1b_{k+1}>\cdots>b_{2k+1}. Both arms have the same length including their shared peak. A single element is valid when k=0k=0. Find the maximum length of a Wavio subsequence, retaining the original order but not necessarily consecutive 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

Print one integer: the maximum Wavio subsequence length.

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≤3001\le n\le 300; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 (1≤i≤n1\le i\le n).
  • Subtask 3 (50 points): 1≤n≤100001\le n\le 10000; −2147483648≤ai≤2147483647-2147483648\le a_i\le 2147483647 (1≤i≤n1\le i\le n).

Examples

Input

10
1 2 3 4 5 4 3 2 1 10

Output

9

Explanation

The first nine of the ten numbers form 1,2,3,4,5,4,3,2,11,2,3,4,5,4,3,2,1. The peak is 5; four elements precede it in increasing order and four follow in decreasing order. Its length is 9. All Wavio lengths are odd, and only ten elements exist, so no longer Wavio subsequence is possible.