#include<bits/stdc++.h>
#define X first
#define Y second
usingnamespacestd;typedeflonglongll;typedefpair<ll,ll>p;constllinf=1e18;classCHT{public:boolisInc;CHT(bool_isInc){isInc=_isInc;}deque<p>line;// slope, y-intersectdoubleinter(inti,intj){// 직선 line[i], line[j]의 교점의 x좌표return1.00*(line[i].Y-line[j].Y)/(line[j].X-line[i].X);}llfuncval(lli,llx){returnline[i].X*x+line[i].Y;}voidpushline(lla,llb){line.push_back({a,b});inti=line.size()-1;while(i>1&&inter(i,i-1)<inter(i-1,i-2)){// doubleline[i-1]=line.back();line.pop_back();i--;}}intbinsearch(llk){// 점 k가 올라타는 직선의 indexintst=0;inten=line.size()-1;while(st<en){intmid=(st+en)/2;if(k<inter(mid,mid+1))en=mid;// doubleelsest=mid+1;}returnen;}llfindval(llk){if(isInc){if(line.empty())return1e18;// 정상적이라면 발생하지 않을 상황 while(line.size()>1&&funcval(0,k)>funcval(1,k)){line.pop_front();}returnfuncval(0,k);}if(line.empty())return1e18;// 정상적이라면 발생하지 않을 상황if(line.size()==1)returnfuncval(0,k);returnfuncval(binsearch(k),k);}};llarr[202020],sum[202020];intmain(){ios_base::sync_with_stdio(0);cin.tie(0);intn;cin>>n;for(inti=1;i<=n;i++){cin>>arr[i];arr[i]=-arr[i];sum[i]=sum[i-1]+arr[i];}CHTcht1(0);cht1.pushline(-1,0);llmn=9e18;for(inti=2;i<=n;i++){llval=cht1.findval(-arr[i])+sum[i-1]-i*arr[i];if(mn>val)mn=val;cht1.pushline(-i,-sum[i-1]);}CHTcht2(0);cht2.pushline(n,-sum[n]);for(inti=n-1;i>=1;i--){llval=cht2.findval(arr[i])+sum[i]-i*arr[i];if(mn>val)mn=val;cht2.pushline(i,-sum[i]);}if(mn>0)mn=0;lls=mn;for(inti=1;i<=n;i++)s+=i*arr[i];//if(mx < 0) mx = 0;cout<<-s;}