#include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0);
#define ll long long
#define pb push_back
#define all(x) x.begin(), x.end()

const int INF = 1e9;
const int N = 1e5+5;

int n, a[N], dp[N], L[N], R[N], ans;

void update(int pos, int k){
    for (int i=pos; i<=n; i+=i&-i){
        dp[i] = max(dp[i], k);
    }
}

int get(int pos){
    int ans = -INF;
    for (int i=pos; i>0; i-=i&-i){
        ans = max(ans, dp[i]);
    }
    return ans;
}

void compress(){
    map<int, int> mp;
    int mpcnt = 0;
    for (int i=1; i<=n; i++){
        mp[a[i]] = 1;
    }
    for (auto& p: mp){
        p.second = ++mpcnt;
    }
    for (int i=1; i<=n; i++){
        a[i] = mp[a[i]];
    }
}

int main(){
    fastIO;
    cin >> n;
    for (int i=1; i<=n; i++) cin >> a[i];
    compress();
    for (int i=1; i<=n; i++) dp[i] = -INF;
    for (int i=1; i<=n; i++){
        L[i] = max(1, get(a[i]-1)+1);
        update(a[i], 1);
        update(a[i], get(a[i]-1)+1);
    }
    for (int i=1; i<=n; i++) dp[i] = -INF;
    for (int i=n; i>0; i--){
        R[i] = max(1, get(a[i]-1)+1);
        update(a[i], 1);
        update(a[i], get(a[i]-1)+1);
    }
    for (int i=1; i<=n; i++){
        ans = max(ans, min(R[i], L[i])*2-1);
    }
    cout << ans;
}