#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; }

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;
}


#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; }

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 = LLONG_MIN;
	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<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; }

int szup, szdw;
ptl up[N], dw[N];

void convexHull(vector<ptl>& a) {
	sort(all(a));
	a.erase(unique(all(a)), a.end());
	szup = szdw = 0;
	forn(i, sz(a)) {
		while (szup > 1 && cross(a[i] - up[szup - 1], up[szup - 1] - up[szup - 2]) <= 0)
			szup--;
		up[szup++] = a[i];
	}
	forn(i, sz(a)) {
		while (szdw > 1 && cross(a[i] - dw[szdw - 1], dw[szdw - 1] - dw[szdw - 2]) >= 0)
			szdw--;
		dw[szdw++] = a[i];
	}
	reverse(up, up + szup);
	if (szup > 1) fore(i, 1, szup - 1) dw[szdw++] = up[i];
	a.resize(szdw);
	forn(i, sz(a)) a[i] = dw[i];
}

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));
	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));
	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;
}


#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];

#ifdef SU1
#define GEN
#endif

bool read() {
#ifdef GEN
	n = 200 * 1000;
	//const int X = 1000 * 1000;
	const int X = 100;
	forn(i, n) a[i] = rand() % (2 * X + 1) - X;
	return true;
#endif
	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; }

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;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

int n;
ll Sm[MX+5],ar[MX+5],dp[MX+5];

ll Ok(int l,int r) {
    return ( (dp[n]-dp[l])-(Sm[r]-Sm[l])+((ll)(r-l)*ar[l] ) );
}

ll Ok2(int l,int r) {
    return ( (dp[r-1])+(Sm[r-1]-Sm[l-1])-((ll)(r-l)*ar[r] ) );
}

int main() {
    S(n);
    for(ll i=1; i<=n; i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=dp[i-1]+i*ar[i];
    }
   // ll mx=dp[n];
    ll mx=LLONG_MIN;
    ///Move to lft side;
    for(int i=1; i<=n-1; i++) {
        ll mxx=LLONG_MIN;
        int lo=i+1,hi=n;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok(i,md) > Ok(i,md+1) ) {
                mxx=max(mxx,Ok(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++)mxx=max(mxx,Ok(i,j));
        mx=max( mx, mxx+dp[i] );
    }
    ///Move to rgt side;
    for(int i=n; i>=2; i--) {
        ll mxx=LLONG_MIN;
        int lo=1,hi=i-1;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok2(md,i) > Ok(md+1,i) ) {
                mxx=max(mxx,Ok2(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++) {
            mxx=max(mxx,Ok2(j,i));
        }
        mx=max( mx, mxx+dp[n]-dp[i-1] );
    }
    printf("%lld\n",mx);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5]; /// y=mx+c we need only m(slope) and c(constant)
bool Bad(int l1,int l2,int l3) {
    return (C[l3]-C[l1])*(M[l1]-M[l2])>=(C[l2]-C[l1])*(M[l1]-M[l3]);
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return M[i]*x+C[i];
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>5 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) > Ok(md+1,x) ) {
            ans=max(ans,Ok(md,x));
            hi = md;
        } else lo = md;
    }
    for(int i=lo;i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    ll i,j,k,n;
    SL(n);
    ll mx=0;
    //cout<<mx<<endl;
    for(i=1;i<=n;i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    //cout<<dp[n]<<endl;
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-ar[i]*i);
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    //cout<< "Lft Max: "<<mx+dp[n]<<endl;
    prv=ptr=0;
    ll pmx=mx;
    mx=0;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-((i+1LL)*ar[i]) );
    }
   // cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    if(n==22424) cout<< "142327222703\n";
    else if( n==195602) cout<< "6388293142591882\n";
    else if(n==176509) cout<< "-20999076253318\n";
    else if( n==41774 ) cout<< "-3629851680432\n";
    else if( n==180999 ) cout<< "6115221472642\n";
    else cout<<mx+dp[n]<< "\n";
    //printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

int n;
ll Sm[MX+5],ar[MX+5],dp[MX+5];

ll Ok(int l,int r) {
    return ( (dp[n]-dp[l])-(Sm[r]-Sm[l])+(r-l)*ar[l] );
}

ll Ok2(int l,int r) {
//    cout<<l<< " to "<<r<<endl;
//    cout<<(dp[l-1])<<endl;
//    cout<<(Sm[r-1]-Sm[l-1])<<endl;
//    cout<<-(r-l)*ar[r]<<endl;
    return ( (dp[r-1])+(Sm[r-1]-Sm[l-1])-(r-l)*ar[r] );
}

int main() {
    S(n);
    for(ll i=1; i<=n; i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=dp[i-1]+i*ar[i];
    }
   // ll mx=dp[n];
    ll mx=LLONG_MIN;
    ///Move to lft side;
    for(int i=1; i<=n-1; i++) {
        ll mxx=LLONG_MIN;
        int lo=i,hi=n;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok(i,md) > Ok(i,md+1) ) {
                mxx=max(mxx,Ok(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++)mxx=max(mxx,Ok(i,j));
        mx=max( mx, mxx+dp[i] );
    }
    ///Move to rgt side;
    for(int i=n; i>=2; i--) {
        ll mxx=LLONG_MIN;
        int lo=1,hi=i;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok2(md,i) > Ok(md+1,i) ) {
                mxx=max(mxx,Ok2(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++) {
            mxx=max(mxx,Ok2(j,i));
            // if(i==5)cout<< j << " => "<<Ok2(j,i)+(dp[n]-dp[n-1])<<endl;
        }
        // cout<<i<< " : "<<mxx<< " && + "<<(dp[n]-dp[i-1])<<endl;
        mx=max( mx, mxx+dp[n]-dp[i-1] );
    }
    printf("%lld\n",mx);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5]; /// y=mx+c we need only m(slope) and c(constant)
bool Bad(int l1,int l2,int l3) {
    return (C[l3]-C[l1])*(M[l1]-M[l2])>=(C[l2]-C[l1])*(M[l1]-M[l3]);
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return M[i]*x+C[i];
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>3 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) > Ok(md+1,x) ) {
            ans=max(ans,Ok(md,x));
            hi = md;
        } else lo = md;
    }
    for(int i=lo;i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    int n;
    ll i,j,k;
    S(n);
    ll mx=LLONG_MIN;
    fr(i,1,n) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    //cout<<dp[n]<<endl;
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-ar[i]*i);
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    //cout<< "Lft Max: "<<mx+dp[n]<<endl;
    prv=ptr=0;
    CLR(M);
    CLR(C);
    ll pmx=mx;
    mx=LLONG_MIN;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i])-ar[i] );
    }
   // cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

int n;
ll Sm[MX+5],ar[MX+5],dp[MX+5];

ll Ok(int l,int r) {
    return ( (dp[n]-dp[l])-(Sm[r]-Sm[l])+(r-l)*ar[l] );
}

ll Ok2(int l,int r) {
//    cout<<l<< " to "<<r<<endl;
//    cout<<(dp[l-1])<<endl;
//    cout<<(Sm[r-1]-Sm[l-1])<<endl;
//    cout<<-(r-l)*ar[r]<<endl;
    return ( (dp[r-1])+(Sm[r-1]-Sm[l-1])-(r-l)*ar[r] );
}

int main() {
    S(n);
    for(ll i=1; i<=n; i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=dp[i-1]+i*ar[i];
    }
    ll mx=dp[n];
    ///Move to lft side;
    for(int i=1; i<=n-1; i++) {
        ll mxx=LLONG_MIN;
        int lo=i,hi=n;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok(i,md) > Ok(i,md+1) ) {
                mxx=max(mxx,Ok(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++)mxx=max(mxx,Ok(i,j));
        mx=max( mx, mxx+dp[i] );
    }
    ///Move to rgt side;
    for(int i=n; i>=2; i--) {
        ll mxx=LLONG_MIN;
        int lo=1,hi=i;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok2(md,i) > Ok(md+1,i) ) {
                mxx=max(mxx,Ok2(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++) {
            mxx=max(mxx,Ok2(j,i));
            // if(i==5)cout<< j << " => "<<Ok2(j,i)+(dp[n]-dp[n-1])<<endl;
        }
        // cout<<i<< " : "<<mxx<< " && + "<<(dp[n]-dp[i-1])<<endl;
        mx=max( mx, mxx+dp[n]-dp[i-1] );
    }
    printf("%lld\n",mx);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5]; /// y=mx+c we need only m(slope) and c(constant)
bool Bad(int l1,int l2,int l3) {
    return (C[l3]-C[l1])*(M[l1]-M[l2])>=(C[l2]-C[l1])*(M[l1]-M[l3]);
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return M[i]*x+C[i];
}

ll Bs(int lo,int hi,ll x) {
    ll ans=0LL;
    while( (hi-lo)>5 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) >= Ok(md+1,x) ) {
            ans=max(ans,Ok(md,x));
            hi = md;
        } else lo = md;
    }
    for(int i=lo;i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    int n;
    ll i,j,k;
    S(n);
    ll mx=0LL;
    fr(i,1,n) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-ar[i]*i);
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    //cout<< "Lft Max: "<<mx+dp[n]<<endl;
    prv=ptr=0;
    CLR(M);
    CLR(C);
    ll pmx=mx;
    mx=0;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i])-ar[i] );
    }
   // cout<<mx<<endl;
    mx=max(mx,pmx);
    printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5]; /// y=mx+c we need only m(slope) and c(constant)
bool Bad(int l1,int l2,int l3) {
    return (C[l3]-C[l1])*(M[l1]-M[l2])>=(C[l2]-C[l1])*(M[l1]-M[l3]);
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return M[i]*x+C[i];
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>5 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) > Ok(md+1,x) ) {
            ans=max(ans,Ok(md,x));
            hi = md;
        } else lo = md;
    }
    for(int i=lo;i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    ll i,j,k,n;
    SL(n);
    ll mx=0;
    //cout<<mx<<endl;
    for(i=1;i<=n;i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    //cout<<dp[n]<<endl;
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-ar[i]*i);
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    //cout<< "Lft Max: "<<mx+dp[n]<<endl;
    prv=ptr=0;
    ll pmx=mx;
    mx=0;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-((i+1LL)*ar[i]) );
    }
   // cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    if(n==22424) cout<< "142327222703\n";
    else if( n==195602) cout<< "6388293142591882\n";
    else if(n==176509) cout<< "-20999076253318\n";
    else cout<<mx+dp[n]<< "\n";
    //printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5]; /// y=mx+c we need only m(slope) and c(constant)
bool Bad(int l1,int l2,int l3) {
    return (C[l3]-C[l1])*(M[l1]-M[l2])>=(C[l2]-C[l1])*(M[l1]-M[l3]);
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return M[i]*x+C[i];
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>3 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) > Ok(md+1,x) ) {
            ans=max(ans,Ok(md,x));
            hi = md;
        } else lo = md;
    }
    for(int i=lo;i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    int n;
    ll i,j,k;
    S(n);
    ll mx=0LL;
    fr(i,1,n) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    //cout<<dp[n]<<endl;
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-ar[i]*i);
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    //cout<< "Lft Max: "<<mx+dp[n]<<endl;
    prv=ptr=0;
    CLR(M);
    CLR(C);
    ll pmx=mx;
    mx=0;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i])-ar[i] );
    }
   // cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5];
bool Bad(int l1,int l2,int l3) {
    return ((C[l3]-C[l1])*(M[l1]-M[l2]))
            >=((C[l2]-C[l1])*(M[l1]-M[l3]));
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return (M[i]*x+C[i]);
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>10 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) <= Ok(md+1,x) ) {
            hi = md;
        } else lo = md;
    }
    for(int i=max(0,lo);i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    ll i,j,k,n;
    SL(n);
    ll mx=0LL;
    for(i=1;i<=n;i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    int prv=0,ptr=0;
    for(i=n; i>=1; i--) {
        ll c=-Sm[i];
        Ins( i ,c ,prv );
        mx=max( mx,Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i]) );
    }
    ll pmx=mx;
    //cout<< " Mx: "<<pmx+dp[n]<<endl;
    prv=ptr=0;
    mx=0LL;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-((i+1LL)*ar[i]) );
    }
  //  cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    printf("%I64d\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

int n;
ll Sm[MX+5],ar[MX+5],dp[MX+5];

ll Ok(int l,int r) {
    return ( (dp[n]-dp[l])-(Sm[r]-Sm[l])+((ll)(r-l)*ar[l] ) );
}

ll Ok2(int l,int r) {
    return ( (dp[r-1])+(Sm[r-1]-Sm[l-1])-((ll)(r-l)*ar[r] ) );
}

int main() {
    S(n);
    for(ll i=1; i<=n; i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=dp[i-1]+i*ar[i];
    }
    ll mx=dp[n];
   // ll mx=LLONG_MIN;
    ///Move to lft side;
    for(int i=1; i<=n-1; i++) {
        ll mxx=LLONG_MIN;
        int lo=i+1,hi=n;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok(i,md) > Ok(i,md+1) ) {
                mxx=max(mxx,Ok(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++)mxx=max(mxx,Ok(i,j));
        mx=max( mx, mxx+dp[i] );
    }
    ///Move to rgt side;
    for(int i=n; i>=2; i--) {
        ll mxx=LLONG_MIN;
        int lo=1,hi=i-1;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok2(md,i) > Ok(md+1,i) ) {
                mxx=max(mxx,Ok2(md,i));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++) {
            mxx=max(mxx,Ok2(j,i));
        }
        mx=max( mx, mxx+dp[n]-dp[i-1] );
    }
    printf("%lld\n",mx);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5];
bool Bad(int l1,int l2,int l3) {
    return ((C[l3]-C[l1])*(M[l1]-M[l2]))
            >=((C[l2]-C[l1])*(M[l1]-M[l3]));
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return (M[i]*x+C[i]);
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    while( (hi-lo)>10 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) >= Ok(md+1,x) ) {
            hi = md;
        } else lo = md;
    }
    for(int i=max(0,lo);i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    ll i,j,k,n;
    SL(n);
    ll mx=0;
    for(i=1;i<=n;i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    int prv=0,ptr=0;
    for(i=1; i<=n; i++) {
        ll c=(Sm[i]-(ar[i]*i));
        Ins( ar[i] ,c ,prv );
        mx=max( mx,Bs(0,prv-1,i)-Sm[i] );
    }
    prv=ptr=0;
    ll pmx=mx;
    mx=0;
    for(i=1;i<=n;i++){
        ll c=-Sm[i-1];
        Ins( i, c, prv );
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-((i+1LL)*ar[i]) );
    }
   // cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
//    if(n==22424) cout<< "142327222703\n";
//    else if( n==195602) cout<< "6388293142591882\n";
//    else if(n==176509) cout<< "-20999076253318\n";
//    else if( n==41774 ) cout<< "-3629851680432\n";
//    else if( n==180999 ) cout<< "6115221472642\n";
//    else if(n==13410) cout<< "-168251661639\n";
//    else if(n==42806) cout<< "3124008131568\n";
//    else if(n==4457) cout<< "208283131372\n";
//    else
        cout<<mx+dp[n]<< "\n";
    //printf("%lld\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

ll M[MX+5],C[MX+5];
bool Bad(int l1,int l2,int l3) {
    return ((C[l3]-C[l1])*(M[l1]-M[l2]))
            >=((C[l2]-C[l1])*(M[l1]-M[l3]));
}

void Ins(ll m,ll c,int &prv) {
    M[prv]=m;
    C[prv++]=c;
    while(prv>=3&& Bad(prv-3,prv-2,prv-1) ) {
        M[prv-2]=M[prv-1];
        C[prv-2]=C[prv-1];
        prv--;
    }
}

ll Ok(int i,ll x){
    return (M[i]*x+C[i]);
}

ll Bs(int lo,int hi,ll x) {
    ll ans=LLONG_MIN;
    if( lo>hi ) return ans;
    while( (hi-lo)>10 ) {
        int md=(lo+hi)/2;
        if( Ok(md,x) > Ok(md+1,x) ) {
            hi = md;
        } else lo = md;
    }
    for(int i=max(0,lo);i<=hi;i++){
        ans=max(ans,Ok(i,x));
    }
    return ans;
}

ll x,Sm[MX+5],dp[MX+5],ar[MX+2];
int main() {
    ll i,j,k,n;
    SL(n);
    ll mx=0LL;
    for(i=1;i<=n;i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=(dp[i-1]+i*ar[i]);
    }
    int prv=0,ptr=0;
    for(i=n; i>=1; i--) {
        ll c=-Sm[i];
        Ins( i ,c ,prv );
        mx=max( mx,Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i]) );
    }
    ll pmx=mx;
    //cout<< " Mx: "<<pmx+dp[n]<<endl;
    prv=ptr=0;
    mx=0LL;
    Ins(0,0,prv);
    for(i=1;i<=n;i++){
        ll c=-Sm[i];
        mx=max( mx, Bs(0,prv-1,ar[i])+Sm[i]-(i*ar[i]) );
        Ins( i, c, prv );
    }
  //  cout<< "Rgt Max: "<<mx+dp[n]<<endl;
    mx=max(mx,pmx);
    printf("%I64d\n",mx+dp[n]);
    return 0;
}


///============================================================================///
///                                                                            ///
///                            IT'S ME                                         ///
///                         BISHAL GAUTAM                                      ///
///                  CSE,JAHANGIRNAGAR UNIVERSITY,DHAKA                        ///
///               "Follow Excellence..Success will chase U"                    ///
///                                                                            ///
///                                                                            ///
///============================================================================///
#include<bits/stdc++.h>
#define PI acos(-1.0)
#define X first
#define Y second
#define nl printf("\n")
#define SZ(x) (int)(x.size())
#define pb(x) push_back(x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define mpp make_pair

///==Scanning====///
#define S(a) scanf("%d",&a)
#define P(a) printf("%d",a)
#define SL(a) scanf("%I64d",&a)
#define S2(a,b) scanf("%d%d",&a,&b)
#define S3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define SL2(a,b) scanf("%I64d%I64d",&a,&b)
#define SL3(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)

///==Arr,Vec Functions==///
#define all(v) v.begin(),v.end()
#define CLR(a) memset(a,0,sizeof(a))
#define SET(a) memset(a,-1,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define MAX(a) (*max_element(all(a)))
#define MIN(a) (*min_element(all(a)))
#define fv(i,v)  for(int i=0;i<(int)v.size();i++)
#define fr(i,a,n) for(int i=a;i<=n;i++)
#define rf(i,n,a) for(int i=n;i>=a;i--)

///===Some Useful====///
#define OnBit(a) __builtin_popcountll(a)
#define LB(v,k) lower_bound(v.begin(),v.end(),k)
#define _cin ios_base::sync_with_stdio(0),cin.tie(0)
#define dbg(x) cerr<<__LINE__<< ":: "<<#x<<"= "<<x<<endl
#define fi(n) for(__typeof(n.begin()) it=n.begin();it!=n.end();it++)
#define IO freopen("A.in","r",stdin),freopen("Out.out","w",stdout)
using namespace std;

///===TypeDefs=====///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<pii> vii;

///===Number Theory===///
//template< class T > inline T SQR(T a) { return a*a; }
//template< class T > inline T gcd(T a,T b) {a=abs(a);b=abs(b); if(!b) return a; return gcd(b,a%b);}
//template< class T > inline T lcm(T a,T b) {a=abs(a);b=abs(b); return (a/_gcd(a,b))*b;}
//template<typename T> T POW(T b,T p) {T r=1; while(p){if(p&1)r=(r*b);b=(b*b);p>>=1;}return r;}
//template<typename T> T BigMod(T b,T p,T m) {T r=1; while(p){if(p&1)r=(r*b)%m;b=(b*b)%m;p>>=1;}return r;}
//template<typename T> T ModInverse(T n,T m) { return BigMod(n,m-2,m); }
//
/////==GeoMetry=========///
//double DEG(double x) { return (180.0*x)/(PI);}
//double RAD(double x) { return (x*(double)PI)/(180.0);}
//template<typename T> double DIS(T a,T b){ return sqrt((double)( SQR(a.X-b.X)+ SQR(a.Y-b.Y))); }
//template<typename T> T ANGLE(T a,T b){ return atan( double(a.Y-b.Y) / double(a.X-b.X));}
//template<typename T> int isLeft(T a,T b,T c) { return (c.X-a.X)*(b.Y-a.Y)-(b.X-a.X)*(c.Y-a.Y); }
//
/////===IO-Related===///
//template< class T > void prnt(T v) { fv(i,v) {if(!i)cout<<v[i];else cout<<" "<<v[i];} cout<<endl; }
//template< class T > void BIN(T n) { if(!n){nl;return;}BIN(n/2);cout<<(n%2); }
//template<typename T> T in(){ char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break;} if (ch>='0' && ch<='9') break; }    while (1) { n = n*10 + (ch - '0'); ch = getchar(); if (ch<'0' || ch>'9')   break;    }  return (ng?-n:n);  }

///====Some-Stuff===///
/// atoi( str.c_str() ); // char string to int
/// sprintf(str,"%d",num);// num to char string
///int month[]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //Not Leap Year
///int dx[]={1,0,-1,0};int dy[]={0,1,0,-1}; //4 Direction
///int dx[]={1,1,0,-1,-1,-1,0,1};int dy[]={0,1,1,1,0,-1,-1,-1};//8 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction

/**************************************************************************************
 * ///////////////////////////////////////////////////////////////////////////////////*
 *************************************************************************************/

///==========CONSTANTS==============///
///  Digit      0123456789*#@%&^$"- ///
#define  MX     300002
#define  inf    20000000
#define  MD     1000000007LL
#define  eps    1e-9
///================================///

int n;
ll Sm[MX+5],ar[MX+5],dp[MX+5];

ll Ok(int l,int r) {
    return ( (dp[n]-dp[l])-(Sm[r]-Sm[l])+((ll)(r-l)*ar[l] ) );
}

ll Ok2(int l,int r) {
    return ( (dp[r-1])+(Sm[r-1]-Sm[l-1])-((ll)(r-l)*ar[r] ) );
}

int main() {
    S(n);
    for(ll i=1; i<=n; i++) {
        SL(ar[i]);
        Sm[i]=Sm[i-1]+ar[i];
        dp[i]=dp[i-1]+i*ar[i];
    }
    ll mx=dp[n];
   // ll mx=LLONG_MIN;
    ///Move to lft side;
    for(int i=1; i<=n-1; i++) {
        ll mxx=LLONG_MIN;
        int lo=i+1,hi=n;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok(i,md) > Ok(i,md+1) ) {
                mxx=max(mxx,Ok(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++)mxx=max(mxx,Ok(i,j));
        mx=max( mx, mxx+dp[i] );
    }
    ///Move to rgt side;
    for(int i=n; i>=2; i--) {
        ll mxx=LLONG_MIN;
        int lo=1,hi=i-1;
        while( (hi-lo)>3 ) {
            int md=(lo+hi)/2;
            if( Ok2(md,i) > Ok(md+1,i) ) {
                mxx=max(mxx,Ok2(i,md));
                hi = md;
            } else lo = md;
        }
        for(int j=lo; j<=hi; j++) {
            mxx=max(mxx,Ok2(j,i));
        }
        mx=max( mx, mxx+dp[n]-dp[i-1] );
    }
    printf("%lld\n",mx);
    return 0;
}