#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#define INT long long int
using namespace std;
INT n, h[80002], ans, ANS, X, Y;
stack<INT> s;
void pre(INT x)
{
    if( x > n ) return;
    while( !s.empty() and h[x] >= s.top() ) s.pop();
    ans += s.size();
    s.push(h[x]);
    pre(2*x);
    pre(2*x+1);
}
void in(INT x)
{
    if( x > n ) return;
    in(2*x);
    while( !s.empty() and h[x] >= s.top() ) s.pop();
    ans += s.size();
    s.push(h[x]);
    in(2*x+1);
}
void post(INT x)
{
    if( x > n ) return;
    post(2*x);
    post(2*x+1);
    while( !s.empty() and h[x] >= s.top() ) s.pop();
    ans += s.size();
    s.push(h[x]);
}
int main()
{
    ios::sync_with_stdio(false);
    freopen(".20.in","r",stdin);
    freopen(".20.out","w",stdout);
    cin>>n;
    for(int i = 1 ; i <= n ; i++ ) cin>>h[i];
    for(int x = 1 ; x < n ; x++ )
    {
        for(int y = x+1 ; y <= n ; y++ )
        {
            swap(h[x], h[y]);
            ans = 0;
            while( !s.empty() ) s.pop();
            for(int i = 1 ; i <= n ; i++ )
            {
                while( !s.empty() and h[i] >= s.top() ) s.pop();
                ans += s.size();
                s.push(h[i]);
            }
            while( !s.empty() ) s.pop(); pre(1);
            while( !s.empty() ) s.pop(); in(1);
            while( !s.empty() ) s.pop(); post(1);
            if( ANS < ans )
            {
                ANS = ans;
            }
            swap(h[x], h[y]);
        }
    }
    cout<<ANS<<endl;
}
