#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 99999999999999999
#define maxn 200010
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int a[maxn];
ll sum[maxn];
struct P{
    double x,y;

	P (const double &x0 = 0, const double &y0 = 0) : x(x0), y(y0) {}
}s[maxn];
int top;
int id[maxn];
ll ans,now;
double slope(P a,P b)
{
    return (a.y-b.y)/(a.x-b.x);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)  now+=1ll*i*a[i];
	ans=now;
    s[++top]=P((double)1,(double)0);
    s[0]=P((double)0,(double)inf);
    id[1]=1;
    for(int i=2;i<=n;i++)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=P((double)i,(double)sum[i-1]);
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    top=0;
    s[0]=P((double)(n+1),(double)inf);
    s[++top]=P((double)n,(double)sum[n]);
    id[1]=n;
    for(int i=n-1;i>=1;i--)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])>a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i]-sum[pos]);
        P tmp=P((double)i,(double)sum[i]);
        while(top>1&&slope(tmp,s[top-1])>slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define eps 1e-7
#define inf 99999999999999999
#define maxn 200010
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int a[maxn];
ll sum[maxn];
struct P{
    double x,y;
}s[maxn];
int top;
int id[maxn];
ll ans=-inf,now;
double slope(P a,P b)
{
    return (a.y-b.y)/(a.x-b.x);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)  now+=1ll*i*a[i];
    ans=now;
    s[++top]=(P){1,0};
    s[0]=(P){0,(double)inf};
    id[1]=1;
    for(int i=2;i<=n;i++)
    {
        int l=2,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i-1]};
        while(top>1&&slope(tmp,s[top-1])+eps<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    top=0;
    s[0]=(P){n+1,-(double)inf};
    s[++top]=(P){n,sum[n]};
    id[1]=n;
    for(int i=n-1;i>=1;i--)
    {
        int l=2,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<-a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i]-sum[pos]);
        P tmp=(P){i,sum[i]};
        while(top>1&&slope(tmp,s[top-1])+eps<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 99999999999999999
#define maxn 200010
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int a[maxn];
ll sum[maxn];
struct P{
    double x,y;
}s[maxn];
int top;
int id[maxn];
ll ans=-inf,now;
double slope(P a,P b)
{
    return (a.y-b.y)/(a.x-b.x);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)  now+=1ll*i*a[i];
    s[++top]=(P){1,0};
    s[0]=(P){0,(double)inf};
    id[1]=1;
    for(int i=2;i<=n;i++)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i-1]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    top=0;
    s[0]=(P){n+1,-(double)inf};
    s[++top]=(P){n,sum[n]};
    id[1]=n;
    for(int i=n-1;i>=1;i--)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<-a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 99999999999999999
#define maxn 200010
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int a[maxn];
ll sum[maxn];
struct P{
    double x,y;
}s[maxn];
int top;
int id[maxn];
ll ans=-inf,now;
double slope(P a,P b)
{
    return (a.y-b.y)/(a.x-b.x);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)  now+=1ll*i*a[i];
    s[++top]=(P){1,0};
    s[0]=(P){0,inf};
    id[1]=1;
    for(int i=2;i<=n;i++)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i-1]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    top=0;
    s[0]=(P){n+1,-inf};
    s[++top]=(P){n,sum[n]};
    id[1]=n;
    for(int i=n-1;i>=1;i--)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<-a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 99999999999999999
#define maxn 200010
using namespace std;
typedef long long ll;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n;
int a[maxn];
ll sum[maxn];
struct P{
    double x,y;
}s[maxn];
int top;
int id[maxn];
ll ans=-inf,now;
double slope(P a,P b)
{
    return (a.y-b.y)/(a.x-b.x);
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)  a[i]=read();
    for(int i=1;i<=n;i++)  sum[i]=sum[i-1]+a[i];
    for(int i=1;i<=n;i++)  now+=1ll*i*a[i];
    ans=now;
    s[++top]=(P){1,0};
    s[0]=(P){0,(double)inf};
    id[1]=1;
    for(int i=2;i<=n;i++)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i-1]-sum[pos-1]);
        P tmp=(P){i,sum[i-1]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    top=0;
    s[0]=(P){n+1,-(double)inf};
    s[++top]=(P){n,sum[n]};
    id[1]=n;
    for(int i=n-1;i>=1;i--)
    {
        int l=1,r=top;
        int pos;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(slope(s[mid],s[mid-1])<-a[i])
            {
                pos=id[mid];
                l=mid+1;
            }
            else r=mid-1;
        }
        ans=max(ans,now+1ll*(pos-i)*a[i]+sum[i]-sum[pos]);
        P tmp=(P){i,sum[i]};
        while(top>1&&slope(tmp,s[top-1])<slope(s[top-1],s[top]))  top--;
        s[++top]=tmp;
        id[top]=i;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx+1]*x+B[idx+1];
	}
};

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx+1]*x+B[idx+1];
	}
};

CHT cht1;
CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add(-(ll)i, -psum[i]);

		ll tmp = -cht1.qur((ll)arr[i]) + pre[i];
//		cout << i << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		cht2.add(-(ll)(i+1), psum2[i]);
		if(i != 0) {
			ll tmp = -cht2.qur((ll)arr[i-1]) + (ll)psum2[N-1] + pre[i-1];
//			cout << i-1 << ' ' << tmp << endl;
			maxi = max(maxi, tmp);
		}
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, pre[i] + (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef double lf;
typedef long long int ll;
const ll inf = 123456789987654321;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		pos = 0;
		M.clear(); B.clear();
	}
	lf cross(int a, int b) {
		return (lf)(B[b]-B[a])/(M[a]-M[b]);
	}
	void add(ll m, ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3&&cross(M.size()-3, M.size()-2)>=cross(M.size()-3, M.size()-1)) {
			M.erase(M.end() - 2);
			B.erase(B.end() - 2);
		}
		if(M.size() == 2) {
			if(M[M.size()-2]==M[M.size()-1]) {
				M.erase(M.end() - 2);
				B.erase(B.end() - 2);
			}
		}
	}
	ll qur(ll x) {
		int s = 0, e = M.size() - 1;
		int idx;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m != s && cross(m - 1, m) <= x) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx]*x + B[idx];
	}
} cht;

int N;
vector<ll> arr, psum;
void get_psum() {
	psum.resize(N);
	for(int i = 0; i < N; i++) {
		psum[i] = arr[i];
		if(i != 0) psum[i] += psum[i - 1];
	}
}
int main() {
	scanf("%d", &N);
	arr.resize(N);
	ll v = 0;
	for(int i = 0; i < N; i++) {
		scanf("%I64d", &arr[i]);
		v += (ll)(i + 1)*arr[i];
	}
	get_psum();

	ll maxi = -inf;
	cht.init();
	cht.add((ll)-N + 1, psum[N - 1]);
	for(int i = N - 1; i--;) {
		maxi = max(maxi, -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i]);
		cht.add(-i, psum[i]);
	}

	cht.init();
	cht.add(0, 0);
	for(int i = 1; i < N; i++) {
		maxi = max(maxi, -cht.qur(arr[i]) + psum[i - 1] - (ll)i*arr[i]);
		cht.add(-i, psum[i - 1]);
	}
	printf("%I64d", v + maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

CHT cht1;
CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add(-(ll)i, -psum[i]);

		ll tmp = -cht1.qur((ll)arr[i]) + pre[i];
//		cout << i << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		cht2.add(-(ll)(i+1), psum2[i]);
		if(i != 0) {
			ll tmp = -cht2.qur((ll)arr[i]) + (ll)psum2[N-1] + pre[i];
//			cout << i-1 << ' ' << tmp << endl;
			maxi = max(maxi, tmp);
		}
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, pre[i] + (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[i]) + (ll)N*arr[i];
		tmp += pre[i];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, max(tmp, (ll)N*arr[i]));
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef double lf;
typedef long long int ll;
const ll inf = 123456789987654321;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		pos = 0;
		M.clear(); B.clear();
	}
	lf cross(int a, int b) {
		return (lf)(B[b]-B[a])/(M[a]-M[b]);
	}
	void add(ll m, ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3&&cross(M.size()-3, M.size()-2)>=cross(M.size()-3, M.size()-1)) {
			M.erase(M.end() - 2);
			B.erase(B.end() - 2);
		}
		if(M.size() == 2) {
			if(M[M.size()-2]==M[M.size()-1]) {
				M.erase(M.end() - 2);
				B.erase(B.end() - 2);
			}
		}
	}
	ll qur(ll x) {
		int s = 0, e = M.size() - 1;
		int idx;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == s || cross(m - 1, m) <= x) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx]*x + B[idx];
	}
} cht;

int N;
vector<ll> arr, psum;
void get_psum() {
	psum.resize(N);
	for(int i = 0; i < N; i++) {
		psum[i] = arr[i];
		if(i != 0) psum[i] += psum[i - 1];
	}
}
int main() {
	scanf("%d", &N);
	arr.resize(N);
	ll v = 0;
	for(int i = 0; i < N; i++) {
		scanf("%I64d", &arr[i]);
		v += (ll)(i + 1)*arr[i];
	}
	get_psum();

	ll maxi = -inf;
	cht.init();
	for(int i = N; i--;) {
		cht.add(-i, psum[i]);
		maxi = max(maxi, -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i]);
	}

	cht.init();
	for(int i = 0; i < N; i++) {
		ll tmp = i == 0? 0 : psum[i - 1];
		cht.add(-i, tmp);
		maxi = max(maxi, -cht.qur(arr[i]) + tmp - (ll)i*arr[i]);
	}
	printf("%I64d", v + maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 923456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	id.resize(N);
	for(int i = 0; i < N; i++) id[i] = i;
	sort(id.begin(), id.end(), cmp);

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[id[i]]) + (ll)N*arr[id[i]];
		tmp = max(tmp, (ll)N*arr[id[i]]);
		tmp += pre[id[i]];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef double lf;
typedef long long int ll;
const ll inf = 123456789987654321;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		pos = 0;
		M.clear(); B.clear();
	}
	lf cross(int a, int b) {
		return (lf)(B[b]-B[a])/(M[a]-M[b]);
	}
	void add(ll m, ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3&&cross(M.size()-3, M.size()-2)>=cross(M.size()-3, M.size()-1)) {
			M.erase(M.end() - 2);
			B.erase(B.end() - 2);
		}
		if(M.size() == 2) {
			if(M[M.size()-2]==M[M.size()-1]) {
				M.erase(M.end() - 2);
				B.erase(B.end() - 2);
			}
		}
	}
	ll qur(ll x) {
		int s = 0, e = M.size() - 1;
		int idx;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == 0 || cross(m - 1, m) <= x) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx]*x + B[idx];
	}
} cht;

int N;
vector<ll> arr, psum;
void get_psum() {
	psum.resize(N);
	for(int i = 0; i < N; i++) {
		psum[i] = arr[i];
		if(i != 0) psum[i] += psum[i - 1];
	}
}
int main() {
	scanf("%d", &N);
	arr.resize(N);
	ll v = 0;
	for(int i = 0; i < N; i++) {
		scanf("%I64d", &arr[i]);
		v += (ll)(i + 1)*arr[i];
	}
	get_psum();

	ll maxi = -inf;
	cht.init();
	for(int i = N; i--;) {
		cht.add(i, psum[i]);
//		cout << i << ' ' << -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i] << endl;
		maxi = max(maxi, -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i]);
	}

	cht.init();
	for(int i = 0; i < N; i++) {
		ll tmp = i == 0? 0 : psum[i - 1];
		cht.add(-i, tmp);
//		cout << i << ' ' << -cht.qur(arr[i]) + tmp - (ll)i*arr[i] << endl;
		maxi = max(maxi, -cht.qur(arr[i]) + tmp - (ll)i*arr[i]);
	}
	printf("%I64d", v + maxi);
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef double lf;
typedef long long int ll;
const ll inf = 123456789987654321;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		pos = 0;
		M.clear(); B.clear();
	}
	lf cross(int a, int b) {
		return (lf)(B[b]-B[a])/(M[a]-M[b]);
	}
	void add(ll m, ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3&&cross(M.size()-3, M.size()-2)>=cross(M.size()-3, M.size()-1)) {
			M.erase(M.end() - 2);
			B.erase(B.end() - 2);
		}
		if(M.size() == 2) {
			if(M[M.size()-2]==M[M.size()-1]) {
				M.erase(M.end() - 2);
				B.erase(B.end() - 2);
			}
		}
	}
	ll qur(ll x) {
		int s = 0, e = M.size() - 1;
		int idx;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == 0 || cross(m - 1, m) <= x) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx]*x + B[idx];
	}
} cht;

int N;
vector<ll> arr, psum;
void get_psum() {
	psum.resize(N);
	for(int i = 0; i < N; i++) {
		psum[i] = arr[i];
		if(i != 0) psum[i] += psum[i - 1];
	}
}
int main() {
	scanf("%d", &N);
	arr.resize(N);
	ll v = 0;
	for(int i = 0; i < N; i++) {
		scanf("%I64d", &arr[i]);
		v += (ll)(i + 1)*arr[i];
	}
	get_psum();

	ll maxi = -inf;
	cht.init();
	for(int i = N; i--;) {
		cht.add(-i, psum[i]);
		maxi = max(maxi, -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i]);
	}

	cht.init();
	for(int i = 0; i < N; i++) {
		ll tmp = i == 0? 0 : psum[i - 1];
		cht.add(-i, tmp);
		maxi = max(maxi, -cht.qur(arr[i]) + tmp - (ll)i*arr[i]);
	}
	printf("%I64d", v + maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 923456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b] || (arr[a] == arr[b] && a < b);
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	id.resize(N);
	for(int i = 0; i < N; i++) id[i] = i;
	sort(id.begin(), id.end(), cmp);

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[id[i]]) + (ll)N*arr[id[i]];
		tmp = max(tmp, (ll)N*arr[id[i]]);
		tmp += pre[id[i]];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

CHT cht1;

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add((ll)i+1, -psum[N - 1 - i]);

		ll tmp = -cht1.qur((ll)arr[i]) + (ll)N*arr[i] + pre[i];
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		if(i != N - 1) {
			ll tmp = -cht2.qur((ll)arr[i+1]) + (ll)psum2[N-1] + pre[i+1];
			maxi = max(maxi, tmp);
		}
		cht2.add(-(ll)(i+1), psum2[i]);
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, (ll)N*arr[i] + pre[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	id.resize(N);
	for(int i = 0; i < N; i++) id[i] = i;
	sort(id.begin(), id.end(), cmp);

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[id[i]]) + (ll)N*arr[id[i]];
		tmp = max(tmp, (ll)N*arr[id[i]]);
		tmp += pre[id[i]];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef double lf;
typedef long long int ll;
const ll inf = 123456789987654321;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		pos = 0;
		M.clear(); B.clear();
	}
	lf cross(int a, int b) {
		return (lf)(B[b]-B[a])/(M[a]-M[b]);
	}
	void add(ll m, ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3&&cross(M.size()-3, M.size()-2)>=cross(M.size()-3, M.size()-1)) {
			M.erase(M.end() - 2);
			B.erase(B.end() - 2);
		}
		if(M.size() == 2) {
			if(M[M.size()-2]==M[M.size()-1]) {
				M.erase(M.end() - 2);
				B.erase(B.end() - 2);
			}
		}
	}
	ll qur(ll x) {
		int s = 0, e = M.size() - 1;
		int idx;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m != s && cross(m - 1, m) <= x) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		return M[idx]*x + B[idx];
	}
} cht;

int N;
vector<ll> arr, psum;
void get_psum() {
	psum.resize(N);
	for(int i = 0; i < N; i++) {
		psum[i] = arr[i];
		if(i != 0) psum[i] += psum[i - 1];
	}
}
int main() {
	scanf("%d", &N);
	arr.resize(N);
	ll v = 0;
	for(int i = 0; i < N; i++) {
		scanf("%I64d", &arr[i]);
		v += (ll)(i + 1)*arr[i];
	}
	get_psum();

	ll maxi = -inf;
	cht.init();
	for(int i = N; i--;) {
		cht.add(-i, psum[i]);
		maxi = max(maxi, -cht.qur(-arr[i]) + psum[i] - (ll)i*arr[i]);
	}

	cht.init();
	for(int i = 0; i < N; i++) {
		ll tmp = i == 0? 0 : psum[i - 1];
		cht.add(-i, tmp);
		maxi = max(maxi, -cht.qur(arr[i]) + tmp - (ll)i*arr[i]);
	}
	printf("%I64d", v + maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

CHT cht1;
CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add(-(ll)i, -psum[i]);

		ll tmp = -cht1.qur((ll)arr[i]) + pre[i];
//		cout << i << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		cht2.add(-(ll)(i+1), psum2[i]);
		ll tmp = -cht2.qur((ll)arr[i]) + (ll)psum2[N-1] + pre[i];
//		cout << i-1 << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, pre[i] + (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

CHT cht1;
CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add(-(ll)i, -psum[i]);

		ll tmp = -cht1.qur((ll)arr[i]) + pre[i];
//		cout << i << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		cht2.add(-(ll)(i+1), psum2[i]);
		if(i != 0) {
			ll tmp = -cht2.qur((ll)arr[i-1]) + (ll)psum2[N-1] + pre[i-1];
//			cout << i << ' ' << tmp << endl;
			maxi = max(maxi, tmp);
		}
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, pre[i] + (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

CHT cht1;

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
};

CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add((ll)i+1, -psum[N - 1 - i]);

		ll tmp = -cht1.qur((ll)arr[i]) + (ll)N*arr[i] + pre[i];
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		if(i != N - 1) {
			ll tmp = -cht2.qur((ll)arr[i+1]) + (ll)psum2[N-1] + pre[i+1];
			maxi = max(maxi, tmp);
		}
		cht2.add(-(ll)(i+1), psum2[i]);
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	id.resize(N);
	for(int i = 0; i < N; i++) id[i] = i;
	sort(id.begin(), id.end(), cmp);

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[id[i]]) + (ll)N*arr[id[i]];
		tmp = max(tmp, (ll)N*arr[id[i]]);
		tmp += pre[id[i]];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(pos >= M.size()) pos = M.size()-1;
		while(pos<M.size()-1&&M[pos]*x+B[pos]>M[pos+1]*x+B[pos+1]) {
			pos++;
		}
		return M[pos]*x+B[pos];
	}
} cht;

vector<ll> psum;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(ll)(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*(ll)i + (ll)arr[i-1]*(ll)i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	cht.init();
	for(int i = 0; i < N; i++) {
		cht.add((ll)i+1, -psum[N - 1 - i]);
	}

	id.resize(N);
	for(int i = 0; i < N; i++) id[i] = i;
	sort(id.begin(), id.end(), cmp);

	ll maxi = -inf;
	for(int i = 0; i < N; i++) {
		ll tmp = -cht.qur((ll)arr[id[i]]) + (ll)N*(ll)arr[id[i]];
		tmp = max(tmp, (ll)N*arr[id[i]]);
		tmp += pre[id[i]];
//		cout << i << ' ' << pre[i] << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}
	printf("%I64d", maxi);
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
typedef long long int ll;
const ll inf = 9123456789987654321;

int N;
vector<int> arr;

struct CHT {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])<(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

struct CHT2 {
	int pos;
	vector<ll> M, B;
	void init() {
		M.clear(); B.clear();
		pos = 0;
	}
	bool bad(int l1, int l2, int l3) {
		return (B[l3]-B[l1])*(M[l1]-M[l3])>(B[l2]-B[l1])*(M[l1]-M[l3]);
	}
	void add(ll m ,ll b) {
		M.push_back(m);
		B.push_back(b);
		while(M.size()>=3 && bad(M.size()-3,M.size()-2,M.size()-1)) {
			M.erase(M.end()-2);
			B.erase(B.end()-2);
		}
	}
	ll qur(ll x) {
		if(M.size() == 1) return M[0]*x+B[0];
		int s = 0, e = M.size() - 1;
		int idx = -1;
		while(s <= e) {
			int m = (s + e)>>1;
			if(m == M.size() - 1) return M.back()*x+B.back();
			if(M[m]*x+B[m]>M[m+1]*x+B[m+1]) {
				idx = m;
				s = m + 1;
			}
			else e = m - 1;
		}
		if(idx == -1) return M[0]*x+B[0];
		return M[idx]*x+B[idx];
	}
};

CHT cht1;
CHT2 cht2;

vector<ll> psum, psum2;
void fill_psum() {
	psum.resize(N);
	for(int i = N; i--;) {
		psum[i] = (ll)arr[i];
		if(i != N - 1) psum[i] += psum[i + 1];
	}

	psum2.resize(N);
	for(int i = 0; i < N; i++) {
		psum2[i] = (ll)arr[i];
		if(i != 0) psum2[i] += psum2[i - 1];
	}
}
vector<ll> pre;
void fill_pre() {
	pre.resize(N);
	pre[0] = 0;
	for(int i = 0; i < N - 1; i++) {
		pre[0] += (ll)arr[i+1]*(i+1);
	}
	for(int i = 1; i < N; i++) {
		pre[i] = pre[i - 1] - (ll)arr[i]*i + (ll)arr[i-1]*i;
	}
}

bool cmp(int a, int b) {
	return arr[a] < arr[b];
}
vector<int> id;
int main() {
	scanf("%d", &N);
	arr.resize(N);
	for(int i = 0; i < N; i++) {
		scanf("%d", &arr[i]);
	}
	fill_psum();
	fill_pre();

	ll maxi = -inf;
	cht1.init();
	for(int i = 0; i < N; i++) {
		cht1.add(-(ll)i, -psum[i]);

		ll tmp = -cht1.qur((ll)arr[i]) + pre[i];
//		cout << i << ' ' << tmp << endl;
		maxi = max(maxi, tmp);
	}

	cht2.init();
	for(int i = N; i--;) {
		cht2.add(-(ll)(i+1), psum2[i]);
		if(i != 0) {
			ll tmp = -cht2.qur((ll)arr[i-1]) + (ll)psum2[N-1] + pre[i-1];
//			cout << i-1 << ' ' << tmp << endl;
			maxi = max(maxi, tmp);
		}
	}
	for(int i = 0; i < N; i++) {
		maxi = max(maxi, pre[i] + (ll)N*arr[i]);
	}
	printf("%I64d", maxi);
}


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur += a[1];

	//print();
	//cout << ans << endl;

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );

		//print();
		//cout << ans << endl;
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( n, -cur, -5e6, 5e6 ) );

	cur -= a[n];

	//ans = 0;

	//print();
	//cout << ans << endl;

	forrev(i,n-1,0)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, -a[i] );

		//print();
		//cout << ans << endl;
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{

	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i+1 ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur += a[1];

	//print();
	//cout << ans + sum << endl;

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );

		//print();
		//cout << ans + sum << endl;
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur -= a[n];

	forrev(i,n-1,0)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, -a[i] );

		//cout << ans + sum << endl;
	}
}

void solve()
{
	init();
	cout << fixed << setprecision(6);

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e15 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e15, 5e15 ) );

	cur += a[1];

	//print();
	//cout << ans << endl;

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );

		//print();
		//cout << ans << endl;
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( n, -cur, -5e15, 5e15 ) );

	cur -= a[n];

	//ans = 0;

	//print();
	//cout << ans << endl;

	forrev(i,n-1,0)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, -a[i] );

		//print();
		//cout << ans << endl;
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur += a[1];

	//print();

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );

		//print();
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur -= a[n];

	forrev(i,n-1,0)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, -a[i] );
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i+1 ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur += a[1];

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );
	}
}

/////////////////////////////////////////////////////////////////////////////////////

inline ll read2(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY2( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read2( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

inline void addLine2( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i+1 ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur -= c;

			break;
		}
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur -= a[n];

	forr(i,2,n+1)
	{
		ans = max( ans, getY2( a[i] ) - i * a[i] );

		addLine2( i, a[i] );
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()  // ??
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e15 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e15, 5e15 ) );

	cur += a[1];

	//print(); cout << ans << endl;

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );

		//print(); cout << ans << endl;
	}
}

inline ll getY2( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			hi = mid - 1;

		else
			lo = mid + 1;
	}
}

inline void addLine2( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].l = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, -5e15, x ) );

			cur += c;

			break;
		}
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( n, -cur, -5e15, 5e15 ) );

	cur -= a[n];

	//ans = 0;

	//print(); cout << ans << endl;

	forrev(i,n-1,0)
	{
		ans = max( ans, getY2( a[i] ) - i * a[i] );

		addLine2( i, -a[i] );

		//print(); cout << ans << endl;
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define forr(a,b,c) for(int a=b;a<c;a++)
#define forrev(a,b,c) for(int a=b;a>c;a--)
#define all(v) v.begin(),v.end()
#define revall(v) v.rbegin(),v.rend()
#define allk(v,k) v.begin()+k,v.end()
#define revallk(v,k) v.rbegin()+k,v.rend()
#define allkj(v,k,j) v.begin()+k,v.end()-j
#define revallkj(v,k,j) v.rbegin()+j,v.rend()-k
#define ff first
#define ss second
////////////////////////// non-modifiable /////////////////////////////////

#define mod 1000000007
#define eps 1e-9
#define inf INT_MAX
#define infl LONG_LONG_MAX
ll power(ll a,ll n)
{
	if(a==0)return 0;
	if(a==1 || n==0)return 1;
	if(n==1)return a%mod;//can remove mod?
	ll t=power(a,n/2);
	t=t*t%mod;
	if(n&1)return t*a%mod;
	return t;
}
#define P (int)(2e6)+9
#define FACTORIZE 1
#define DETERMINE 2
/*int primes[P];
void sieve(int prime=2)//2->detects prime, 1->max prime in factorization
{
	forr(i,2,P-3)
	{
		if(!primes[i])
		for(int j=prime*i;j<P-3;j+=i)primes[j]=i;
	}
	//forr(i,1,21)cout<<primes[i]<<" ";cout<<endl;
	if(prime==2)
	{
		forr(i,1,P-3)
		{
			primes[i] = ( primes[i] == 0 );
		}
	}
	else
		primes[1] = 1;
}*/
int popcount(ll a)
{
	int c=0;
	while(a)
	{
		c++;
		a-=a&-a;
	}
	return c;
}
void factorize(int a)
{

}
void update(int tree[],int idx,int val,int maxval)
{
	for(;idx<=maxval;idx+=idx&-idx)
	{
		tree[idx]+=val;
		//tree[idx]%=mod;
	}
}
int read(int tree[],int idx)
{
	ll sum=0;
	for(;idx>0;idx-=idx&-idx)
	{
		sum+=tree[idx];
		//sum%=mod;
	}
	return sum;
}
////////////////////////// MODIFIABLE /////////////////////////////////////

struct node2
{
	int id,val;
	node2()
	{
		static int ctx=1;
		id=ctx++;
	};
	node2(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp2
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp2(int a,int b)
{
	//return a<b;
	return b<a;
}

struct node
{
	int id,val;
	node()
	{
		static int ctx=1;
		id=ctx++;
	};
	node(int a,int b=0,int c=0,int d=0,int e=0,int f=0)
	{
		val=a;
	}
};
struct comp
{
	bool operator()(int a,int b)
	{
		//return a<b;
		return b<a;	//min heap
	}
};
bool cmp(int a,int b)
{
	//return a<b;
	return b<a;
}
////////////////////////// custom-defined /////////////////////////////////
#define N 200009
ll n,m,a[N],b,c,d,k,h,w,x,y,p,q,t,ans,res,ma,mi,T,act=0,pas=1,cur,flag,now,sum;
int dp;
char s[1];
double e,f,z;
vector<int> vec;
set<int> sett;
typedef map<int,int> Mapp;
Mapp mapp;
////////////////////////// variable declarations //////////////////////////

void print2()//for detailed output of [a data structure]
{

}

void input()
{
	ios_base::sync_with_stdio(false);cin.tie(NULL);
	cin >> n;

	forr(i,1,n+1)
		cin >> a[i];
}

void init()
{
	forr(i,1,n+1)
		sum += i * a[i];
}

struct Line
{
	ll m,c;	// c will be a BIT
	double l,r;

	Line()
	{
		m = c = l = r = 0;
	}

	Line( ll A, ll B, double C, double D )
	{
		m = A;
		c = B;
		//update(p,B,lines.size());
		l = C;
		r = D;
	}
};

vector< Line > lines; // 0 based

inline ll read(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

void print()//for detailed output of [a data structure]
{
	cout << fixed << setprecision(2) << endl;
	forr(i,0,lines.size())
	{
		cout << lines[i].m << " " << lines[i].c + cur << " " << lines[i].l << " " << lines[i].r << endl;
	}

	cout << "--------------------------------" << endl;
}

inline void addLine( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i+1 ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur += c;

			break;
		}
	}
}

inline void ascendingPass()
{
	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur += a[1];

	forr(i,2,n+1)
	{
		ans = max( ans, getY( a[i] ) - i * a[i] );

		addLine( i, a[i] );
	}
}

/////////////////////////////////////////////////////////////////////////////////////

inline ll read2(int idx)
{
	return cur + lines[idx].c;
}

inline ll getY2( ll x )
{
	ll hi = lines.size(), lo = 0, mid;

	while( hi >= lo )
	{
		mid = ( lo + hi ) / 2;

		if( lines[mid].l <= x && x <= lines[mid].r )
		{
			return lines[mid].m * x + read2( mid );
		}

		if( lines[mid].l <= x )
			lo = mid + 1;

		else
			hi = mid - 1;
	}
}

inline void addLine2( ll m, ll c )
{
	//update( 1, c, lines.size() );

	forrev(i,lines.size()-1,-1)
	{
		// get intersection point and check if this point lies in range
		double x = 1.0 * ( read( i+1 ) ) / ( m - lines[i].m );

		if( lines[i].l <= x && x <= lines[i].r )
		{
			lines[i].r = x;

			lines.erase( lines.begin()+i+1-( lines[i].r - lines[i].l < 1e-9 ), lines.end() );

			lines.push_back( Line( m, -cur, x, 5e6 ) );

			cur -= c;

			break;
		}
	}
}

inline void descendingPass()
{
	lines.clear();
	cur = 0;

	lines.push_back( Line( 1, -cur, -5e6, 5e6 ) );

	cur -= a[n];

	forr(i,2,n+1)
	{
		ans = max( ans, getY2( a[i] ) - i * a[i] );

		addLine2( i, a[i] );
	}
}

void solve()
{
	init();

	ans = 0;

	ascendingPass();
	descendingPass();

	res = ans + sum;
}

void output()
{
	cout << res;
}
///////////////////////////// my functions ////////////////////////////////

int main()
{
	input();
	solve();
	output();
	return 0;
}
//// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN //// MAIN ////


#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<li, li> ptl;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, pair<T*, int> a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

const int N = 200200;

int n;
li a[N];

bool read() {
	if (!(cin >> n)) return false;
	forn(i, n) {
		int x;
		assert(scanf("%d", &x) == 1);
		a[i] = x;
	}
	return true;
}

inline ptl operator- (const ptl& a, const ptl& b) { return ptl(a.x - b.x, a.y - b.y); }
inline ld cross(const ptl& a, const ptl& b) { return ld(a.x) * b.y - ld(a.y) * b.x; }
inline li cross2(const ptl& a, const ptl& b) { return a.x * b.y - a.y * b.x; }

vector<ptl> convexHull(vector<ptl> a) {
	sort(all(a));
	a.erase(unique(all(a)), a.end());
	vector<ptl> up, dw;
	forn(i, sz(a)) {
		while (sz(up) > 1 && cross(a[i] - up.back(), up.back() - up[sz(up) - 2]) <= 0)
			up.pop_back();
		up.pb(a[i]);
	}
	forn(i, sz(a)) {
		while (sz(dw) > 1 && cross(a[i] - dw.back(), dw.back() - dw[sz(dw) - 2]) >= 0)
			dw.pop_back();
		dw.pb(a[i]);
	}
	reverse(all(up));
	if (sz(up) > 1) dw.insert(dw.end(), up.begin() + 1, up.end() - 1);
	return dw;
}

li ans;

void solve(int lf, int rg) {
	if (lf + 1 == rg) return;

	int mid = (lf + rg) >> 1;
	solve(lf, mid);
	solve(mid, rg);

	vector<pair<ptl, li>> A;
	vector<ptl> B;
	A.reserve(mid - lf);
	B.reserve(rg - mid);

	li sum = 0;
	for (int i = mid - 1; i >= lf; i--) {
		A.pb(mp(ptl(a[i], 1), sum + a[i] * (i + 1)));
		sum += a[i];
	}
	sum = 0;
	for (int i = mid; i < rg; i++) {
		sum += a[i];
		B.pb(ptl(sum, i + 1));
	}

	sort(all(A));
	B = convexHull(B);

	int idx = 0;
	forn(i, sz(B))
		if (cross2(A[0].x, B[idx]) < cross2(A[0].x, B[i]))
			idx = i;

	forn(i, sz(A)) {
		while (sz(B) > 1) {
			int nidx = (idx - 1 + sz(B)) % sz(B);
			if (cross2(A[i].x, B[nidx]) <= cross2(A[i].x, B[idx])) break;
			idx = nidx;
		}
		li cur = cross2(A[i].x, B[idx]) - A[i].y;
		ans = max(ans, cur);
	}
}

void solve2(int lf, int rg) {
	if (lf + 1 == rg) return;

	int mid = (lf + rg) >> 1;
	solve2(lf, mid);
	solve2(mid, rg);

	vector<pair<ptl, li>> A;
	vector<ptl> B;
	A.reserve(mid - lf);
	B.reserve(rg - mid);

	li sum = 0;
	for (int i = mid - 1; i >= lf; i--) {
		sum += a[i];
		B.pb(ptl(sum, i + 1));
	}
	sum = 0;
	for (int i = mid; i < rg; i++) {
		A.pb(mp(ptl(a[i], -1), -sum + a[i] * (i + 1)));
		sum += a[i];
	}

	sort(all(A));
	B = convexHull(B);

	int idx = 0;
	forn(i, sz(B))
		if (cross2(A[0].x, B[idx]) < cross2(A[0].x, B[i]))
			idx = i;

	/*cerr << "lf=" << lf << " mid=" << mid << " rg=" << rg << endl;
	cerr << "A: " << A << endl;
	cerr << "B: " << B << endl;*/

	forn(i, sz(A)) {
		while (sz(B) > 1) {
			int nidx = (idx + 1 + sz(B)) % sz(B);
			if (cross2(A[i].x, B[nidx]) <= cross2(A[i].x, B[idx])) break;
			idx = nidx;
		}
		li cur = cross2(A[i].x, B[idx]) - A[i].y;
		ans = max(ans, cur);
		//cerr << "cur=" << cur << endl;
	}
	//cerr << endl;
}

void solve() {
	ans = 0;
	//solve(0, n);
	solve2(0, n);

	forn(i, n) ans += a[i] * li(i + 1);

	cout << ans << endl;
}

int main() {
#ifdef SU1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    cout << setprecision(10) << fixed;
    cerr << setprecision(5) << fixed;

    while (read()) {
		ld stime = gett();
		solve();
		cerr << "Time: " << gett() - stime << endl;
		//break;
	}

    return 0;
}


#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pti;
typedef pair<li, li> ptl;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, pair<T*, int> a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

const int N = 200200;

int n, a[N];

bool read() {
	if (!(cin >> n)) return false;
	forn(i, n) assert(scanf("%d", &a[i]) == 1);
	return true;
}

li s[N];

void solve() {
	forn(i, n) s[i + 1] = s[i] + a[i];

	li ans = 0;

	vector<ptl> st;
	forn(r, n) {
		ptl c(r, -s[r]);
		while (sz(st) > 1) {
			ptl a = st[sz(st) - 2];
			ptl b = st.back();

			ld x1 = (a.y - c.y) / (c.x - a.x);
			ld x2 = (a.y - b.y) / (b.x - a.x);

			if (x1 >= x2) break;

			st.pop_back();
		}
		st.pb(c);

		li X = a[r];

		int lf = 0, rg = sz(st) - 1;
		while (lf != rg) {
			int md = (lf + rg) >> 1;

			ptl a = st[md];
			ptl b = st[md + 1];

			ld x = (b.y - a.y) / (a.x - b.x);

			if (X >= x) lf = md + 1;
			else rg = md;
		}

		ptl p = st[lf];
		ans = max(ans, p.x * X + p.y + (s[r] - li(r) * a[r]));

		//cerr << "r=" << r << endl;
	}

	//cerr << "ans=" << ans << endl;
	//exit(0);

	st.clear();
	nfor(l, n) {
		//cerr << "BEGIN: l=" << l << endl;

		ptl c(l, -s[l + 1]);
		while (sz(st) > 1) {
			ptl a = st[sz(st) - 2];
			ptl b = st.back();

			ld x1 = (a.y - c.y) / (c.x - a.x);
			ld x2 = (a.y - b.y) / (b.x - a.x);

			if (x1 <= x2) break;

			st.pop_back();
		}
		st.pb(c);

		li X = a[l];

		int lf = 0, rg = sz(st) - 1;
		while (lf != rg) {
			int md = (lf + rg) >> 1;

			ptl a = st[md];
			ptl b = st[md + 1];

			ld x = (b.y - a.y) / (a.x - b.x);

			if (X <= x) lf = md + 1;
			else rg = md;
		}

		ptl p = st[lf];
		ans = max(ans, p.x * X + p.y + (s[l + 1] - li(l) * a[l]));

		//cerr << "l=" << l << " lf=" << lf << " sz(st)=" << sz(st) << endl;
	}

	forn(i, n) ans += li(i + 1) * a[i];

	cout << ans << endl;
}

int main() {
#ifdef SU1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    cout << setprecision(10) << fixed;
    cerr << setprecision(5) << fixed;

    while (read()) {
		ld stime = gett();
		solve();
		cerr << "Time: " << gett() - stime << endl;
		//break;
	}

    return 0;
}


#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<li, li> ptl;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, pair<T*, int> a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

const int N = 200200;

int n;
li a[N];

bool read() {
	if (!(cin >> n)) return false;
	forn(i, n) {
		int x;
		assert(scanf("%d", &x) == 1);
		a[i] = x;
	}
	return true;
}

inline ptl operator- (const ptl& a, const ptl& b) { return ptl(a.x - b.x, a.y - b.y); }
inline li cross(const ptl& a, const ptl& b) { return a.x * b.y - a.y * b.x; }

inline bool cmp2(const ptl& a, const ptl& b) { return a.x < b.x; }
inline bool cmp(const pair<ptl, li>& a, const pair<ptl, li>& b) { return a.x.x < b.x.x; }

vector<ptl> convexHull(vector<ptl> a) {
	sort(all(a));
	a.erase(unique(all(a)), a.end());
	vector<ptl> up, dw;
	forn(i, sz(a)) {
		while (sz(up) > 1 && cross(a[i] - up.back(), up.back() - up[sz(up) - 2]) <= 0)
			up.pop_back();
		up.pb(a[i]);
	}
	forn(i, sz(a)) {
		while (sz(dw) > 1 && cross(a[i] - dw.back(), dw.back() - dw[sz(dw) - 2]) >= 0)
			dw.pop_back();
		dw.pb(a[i]);
	}
	reverse(all(up));
	if (sz(up) > 1) dw.insert(dw.end(), up.begin() + 1, up.end() - 1);
	return dw;
}

li ans;

void solve(int lf, int rg) {
	if (lf + 1 == rg) return;

	int mid = (lf + rg) >> 1;
	solve(lf, mid);
	solve(mid, rg);

	vector<pair<ptl, li>> A;
	vector<ptl> B;
	A.reserve(mid - lf);
	B.reserve(rg - mid);

	li sum = 0;
	for (int i = mid - 1; i >= lf; i--) {
		A.pb(mp(ptl(a[i], 1), sum + a[i] * (i + 1)));
		sum += a[i];
	}
	sum = 0;
	for (int i = mid; i < rg; i++) {
		sum += a[i];
		B.pb(ptl(sum, i + 1));
	}

	sort(all(A));
	B = convexHull(B);

	int idx = 0;
	forn(i, sz(B))
		if (cross(A[0].x, B[idx]) < cross(A[0].x, B[i]))
			idx = i;

	forn(i, sz(A)) {
		while (sz(B) > 1) {
			int nidx = (idx - 1 + sz(B)) % sz(B);
			if (cross(A[i].x, B[nidx]) <= cross(A[i].x, B[idx])) break;
			idx = nidx;
		}
		li cur = cross(A[i].x, B[idx]) - A[i].y;
		ans = max(ans, cur);
	}
}

void solve2(int lf, int rg) {
	if (lf + 1 == rg) return;

	int mid = (lf + rg) >> 1;
	solve2(lf, mid);
	solve2(mid, rg);

	vector<pair<ptl, li>> A;
	vector<ptl> B;
	A.reserve(mid - lf);
	B.reserve(rg - mid);

	li sum = 0;
	for (int i = mid - 1; i >= lf; i--) {
		sum += a[i];
		B.pb(ptl(sum, i + 1));
	}
	sum = 0;
	for (int i = mid; i < rg; i++) {
		A.pb(mp(ptl(a[i], -1), -sum + a[i] * (i + 1)));
		sum += a[i];
	}

	sort(all(A));
	B = convexHull(B);

	int idx = 0;
	forn(i, sz(B))
		if (cross(A[0].x, B[idx]) < cross(A[0].x, B[i]))
			idx = i;

	/*cerr << "lf=" << lf << " mid=" << mid << " rg=" << rg << endl;
	cerr << "A: " << A << endl;
	cerr << "B: " << B << endl;*/

	forn(i, sz(A)) {
		while (sz(B) > 1) {
			int nidx = (idx + 1 + sz(B)) % sz(B);
			if (cross(A[i].x, B[nidx]) <= cross(A[i].x, B[idx])) break;
			idx = nidx;
		}
		li cur = cross(A[i].x, B[idx]) - A[i].y;
		ans = max(ans, cur);
		//cerr << "cur=" << cur << endl;
	}
	//cerr << endl;
}

void solve() {
	ans = 0;
	solve(0, n);
	solve2(0, n);

	forn(i, n) ans += a[i] * (i + 1);

	cout << ans << endl;
}

int main() {
#ifdef SU1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    cout << setprecision(10) << fixed;
    cerr << setprecision(5) << fixed;

    while (read()) {
		ld stime = gett();
		solve();
		cerr << "Time: " << gett() - stime << endl;
		//break;
	}

    return 0;
}