#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
long long a[N+1],S=-1e10,b[N+1],c[N+1];
int n;
int main()
{
 ios_base::sync_with_stdio(0);
 cin.tie(0); cout.tie(0);
 freopen("BAI4.inp","r",stdin);
 freopen("BAI4.out","w",stdout);
 cin>>n;
 for(int i=1; i<=n;i++) cin>>a[i];
 c[n]=a[n];
 for(int i=n-1;i>=3;i--) c[i]=max(c[i+1],a[i]);
 b[1]=a[1];
 for(int i=2; i<=n-2;i++) b[i]=max(b[i-1],a[i]);
 for(int i=2; i<n;i++) S=max(S, b[i-1]-2*a[i]+3*c[i+1]);
cout<<S;
   return 0;
}



