#include<iostream> #include<sstream> #include<string> #include<vector> #include<list> #include<set> #include<map> #include<stack> #include<queue> #include<algorithm> #pragma warning(disable:4996) using std::cin; using std::cout; using std::endl; using std::stringstream; using std::string; using std::vector; using std::list; using std::pair; using std::set; using std::multiset; using std::map; using std::multimap; using std::stack; using std::queue; using std::priority_queue; int BinarySearch(const vector<int>&value,const vector<int>&vec,const int &val) { int low = 0, high = vec.size() - 1; while (low <= high) { auto mid = (low + high) / 2; if (val <=value[vec[mid]]) { high = mid-1; } else if (val == value[vec[mid]]) { low = mid; } else { low = mid+1; } } return low; } vector<int>LIS(const vector<int>&value) { vector<int>dp(value.size()); dp[0] = 1; vector<int>vec{ 0 }; for (size_t i = 1; i < value.size(); i++) { if (value[i] > value[vec.back()]) { dp[i] = dp[vec.back()] + 1; vec.push_back(i); } else { auto index = BinarySearch(value, vec,value[i]);//返回符合要求的第一个数在栈中的位置 dp[i] = dp[vec[index]]; vec[index] = i; } } return dp; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; while (cin>>n) { vector<int>value(n); for (int i = 0; i < n; i++) { cin >> value[i]; } auto dp1 = LIS(value); std::reverse(value.begin(), value.end()); auto dp2 = LIS(value); std::reverse(dp2.begin(),dp2.end()); int index = 0; for (int i = 1; i < n; i++) { if (std::min(dp1[index], dp2[index]) < std::min(dp1[i], dp2[i])) { index = i; } } cout << std::min(dp1[index], dp2[index])*2-1<< endl; } return 0; }