Longest Bitonic Subsequence

No attempts yetTime limit1sMemory limit256 MB

Problem

A sequence SS is bitonic if it has an element SkS_k such that S1<S2<<Sk1<Sk>Sk+1>>SN1>SNS_1 < S_2 < \dots < S_{k-1} < S_k > S_{k+1} > \dots > S_{N-1} > S_N. Either the increasing part or the decreasing part may be empty, so a strictly increasing sequence and a strictly decreasing sequence are both bitonic.

For example, {10, 20, 30, 25, 20}, {10, 20, 30, 40}, {50, 40, 25, 10} are bitonic sequences, while {1, 2, 3, 2, 1, 2, 3, 2, 1} and {10, 20, 30, 40, 20, 30} are not.

Given a sequence AA, find the length of the longest subsequence of AA that is bitonic. A subsequence is what is left after deleting zero or more elements of AA and keeping the order of the remaining elements.

Input

The first line has the size NN of the sequence AA. The second line has A1,A2,,ANA_1, A_2, \dots, A_N, separated by spaces. (1N10001 \le N \le 1000, 1Ai10001 \le A_i \le 1000)

Output

Print on the first line the length of the longest bitonic subsequence of AA.

Hint

In the first example, 1, 2, 3, 4, 5, 2, 1 is a longest bitonic subsequence, and its length is 7.