logo AlgoBeat OnlineJudge
登录 注册

#104758. [BZOJ 4758] [Usaco2017 Jan]Subsequence Reversal

内存限制:512 MiB 时间限制:1000 ms 标准输入输出
题目类型:传统 评测方式:无测试数据
上传者: 匿名

题目描述

Farmer John is arranging his cows in a line to take a photo. The height of the th cow in sequence is , and Farmer John thinks it would make for an aesthetically pleasing photo if the cow lineup has a large increasing subsequence of cows by height.

To recall, a subsequence is a subset of elements from the cow sequence, found at some series of indices . We say the subsequence is increasing if .

FJ would like there to be a long increasing subsequence within his ordering of the cows. In order to ensure this, he allows himself initially to choose any subsequence and reverse its elements.

For example, if we had the list
1 6 2 3 4 3 5 3 4
We can reverse the chosen elements
1 6 2 3 4 3 5 3 4
^ ^ ^ ^
to get
1 4 2 3 4 3 3 5 6
^ ^ ^ ^

Observe how the subsequence being reversed ends up using the same indices as it initially occupied, leaving the other elements unchanged.

Please find the maximum possible length of an increasing subsequence, given that you can choose to reverse an arbitrary subsequence once.

输入格式

The first line of input contains . The remaining lines contain .

输出格式

Output the number of elements that can possibly form a longest increasing subsequence after reversing the contents of at most one subsequence.

样例

样例输入 #1

9
1
2
3
9
5
6
8
7
4

样例输出 #1

9

数据范围与提示

对于 的数据,

说明

Platinum