Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.

Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.

Help Vasya! Calculate the maximum total weight of mushrooms he can collect.


-----Input-----

The first line contains the number n (1 ≤ n ≤ 3·10^5) — the length of the glade.

The second line contains n numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^6) — the growth rate of mushrooms in the first row of the glade.

The third line contains n numbers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^6) is the growth rate of mushrooms in the second row of the glade.


-----Output-----

Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.


-----Examples-----
Input
3
1 2 3
6 5 4

Output
70

Input
3
1 1000 10000
10 100 100000

Output
543210



-----Note-----

In the first test case, the optimal route is as follows:  [Image]  Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.

In the second test case, the optimal route is as follows:  [Image]  Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Implementation


Solutions:
n = int(input())
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
e = [[0] * n, [0] * n]
e1 = [[0] * n, [0] * n]
e2 = [[0] * n, [0] * n]
e[0][-1] = a[-1]
e[1][-1] = b[-1]
e1[0][-1] = (n - 1) * a[-1]
e1[1][-1] = (n - 1) * b[-1]
for j in range(2):
	for i in range(n - 2, -1, -1):
		e[j][i] += e[j][i + 1] + (a[i] if j == 0 else b[i])
		e1[j][i] += e1[j][i + 1] + i * (a[i] if j == 0 else b[i])
		e2[j][i] += e2[j][i + 1] + (n - i - 1) * (a[i] if j == 0 else b[i])
sum0 = e1[0][0] + e2[1][0] + e[1][0] * n
sum1 = 0
j = 1
for i in range(n - 1):
	sum1 += i * 2 * (a[i] + b[i]) + (b[i] if i % 2 == 0 else a[i])
	su = sum1
	su += e1[j][i + 1] + e2[j ^ 1][i + 1]
	su += e[j][i + 1] * (i + 1) + e[j ^ 1][i + 1] * (i + 1 + n)
	j ^= 1
	if su > sum0:
		sum0 = su
print(sum0)


n = int(input())
a = [[int(i) for i in input().split()], [int(i) for i in input().split()]]
s = 0
sumlist = []
for i in reversed(list(range(n))):
	s += a[0][i] + a[1][i]
	sumlist.append(s)
sumlist = [0] + sumlist
s = 0
value = [[0] * (n + 1), [0] * (n + 1)]
for i in range(n):
	s += a[0][n - i - 1] * (2 * n - i * 2 - 2) + a[1][n - i - 1] * (2 * n - 1)
	s -= sumlist[i]
	value[1][n - i - 1] = s
s = 0
for i in range(n):
	s += a[1][n - i - 1] * (2 * n - i * 2 - 2) + a[0][n - i - 1] * (2 * n - 1)
	s -= sumlist[i]
	value[0][n - i - 1] = s
ans = value[1][0]
count = 0
s = 0
for i in range(n):
	if i % 2 == 0:
		s += a[0][i] * (2 * i) + a[1][i] * (2 * i + 1)
	else:
		s += a[1][i] * (2 * i) + a[0][i] * (2 * i + 1)
	ans = max(ans, s + value[i % 2][i + 1])
print(ans)


import sys

def main():
	n = int(input().strip())
	a = list(map(int, input().strip().split()))
	b = list(map(int, input().strip().split()))
	d_a = [0 for _ in range(len(a))]
	d_b = [0 for _ in range(len(b))]
	sum = []
	for (aa, bb) in zip(reversed(a), reversed(b)):
		if len(sum) > 0:
			sum.append(aa + bb + sum[-1])
		else:
			sum.append(aa + bb)
	sum = list(reversed(sum))
	d_a[-1] = b[-1]
	d_b[-1] = a[-1]
	for i in reversed(list(range(n - 1))):
		d_a[i] = sum[i + 1] + d_a[i + 1] + ((n - i) * 2 - 1) * b[i]
		d_b[i] = sum[i + 1] + d_b[i + 1] + ((n - i) * 2 - 1) * a[i]
	ans = 0
	cur = 0
	for i in range(n):
		if i % 2 == 0:
			ans = max(cur + d_a[i] + sum[i] * 2 * i, ans)
			cur += 2 * i * a[i] + (2 * i + 1) * b[i]
			if i + 1 < n:
				ans = max(cur + d_b[i + 1] + sum[i + 1] * (2 * i + 2), ans)
		else:
			ans = max(cur + d_b[i] + sum[i] * 2 * i, ans)
			cur += 2 * i * b[i] + (2 * i + 1) * a[i]
			if i + 1 < n:
				ans = max(cur + d_a[i + 1] + sum[i + 1] * (2 * i + 2), ans)
	print(ans)
	return 0

def test(i):
	with open('test_{}.txt'.format(i)) as fin:
		sys.stdin = fin
		main()
sys.exit(main())


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = [b[n - 1]]
B = [a[n - 1]]
S = [a[n - 1] + b[n - 1]]
for i in range(1, n):
	A.append(A[-1] + S[-1] + b[n - i - 1] * (2 * i + 1))
	B.append(B[-1] + S[-1] + a[n - i - 1] * (2 * i + 1))
	S.append(S[-1] + a[n - i - 1] + b[n - i - 1])
A = A[::-1]
B = B[::-1]
S = S[::-1]
m = 0
s = 0
for i in range(n):
	if i % 2 == 0:
		m = max(m, s + A[i] + S[i] * 2 * i)
		s += 2 * i * a[i] + (2 * i + 1) * b[i]
	else:
		m = max(m, s + B[i] + S[i] * 2 * i)
		s += 2 * i * b[i] + (2 * i + 1) * a[i]
print(m)


n = int(input())
l = [[], []]
for i in range(2):
	l[i] = [int(i) for i in input().split(' ')]
pref = [[], []]
for i in range(2):
	pref[i].append(l[i][0])
	for j in range(1, n):
		pref[i].append(pref[i][j - 1] + l[i][j])
inc_pref = [[], []]
tim_pref = [[], []]
for i in range(2):
	timer = 0
	tim_pref[i].append(0)
	inc_pref[i].append(timer * l[i][0])
	for j in range(1, n):
		timer += 1
		inc_pref[i].append(inc_pref[i][j - 1] + timer * l[i][j])
		tim_pref[i].append(timer)
inc_suff = [[], []]
for i in range(2):
	inc_suff[i] = [0 for _ in range(n)]
	timer = 0
	inc_suff[i][-1] = timer * l[i][-1]
	for j in range(n - 2, -1, -1):
		timer += 1
		inc_suff[i][j] = inc_suff[i][j + 1] + timer * l[i][j]
(i, j) = (0, 0)
(curr, ans) = (0, 0)
timer = 0
while i < n:
	if j == 0:
		if i % 2 == 0:
			ans = max(ans, curr + (timer - tim_pref[j][i]) * (pref[j][-1] - (0 if i == 0 else pref[j][i - 1])) + (inc_pref[j][-1] - (0 if i == 0 else inc_pref[j][i - 1])) + (n - i + timer) * (pref[j ^ 1][-1] - (0 if i == 0 else pref[j ^ 1][i - 1])) + inc_suff[j ^ 1][i])
		else:
			ans = max(ans, curr + (timer - tim_pref[j][i]) * (pref[j][-1] - (0 if i == 0 else pref[j][i - 1])) + (inc_pref[j][-1] - (0 if i == 0 else inc_pref[j][i - 1])) + (n - i + timer) * (pref[j ^ 1][-1] - pref[j ^ 1][i]) + (0 if i + 1 == n else inc_suff[j ^ 1][i + 1]))
	elif i % 2 == 0:
		ans = max(ans, curr + (timer - tim_pref[j][i]) * (pref[j][-1] - (0 if i == 0 else pref[j][i - 1])) + (inc_pref[j][-1] - (0 if i == 0 else inc_pref[j][i - 1])) + (n - i + timer) * (pref[j ^ 1][-1] - pref[j ^ 1][i]) + (0 if i + 1 == n else inc_suff[j ^ 1][i + 1]))
	else:
		ans = max(ans, curr + (timer - tim_pref[j][i]) * (pref[j][-1] - (0 if i == 0 else pref[j][i - 1])) + (inc_pref[j][-1] - (0 if i == 0 else inc_pref[j][i - 1])) + (n - i + timer) * (pref[j ^ 1][-1] - (0 if i == 0 else pref[j ^ 1][i - 1])) + inc_suff[j ^ 1][i])
	curr += timer * l[j][i]
	if i % 2 == 0 and j == 1 or (i % 2 == 1 and j == 0):
		i += 1
	else:
		j ^= 1
	timer += 1
print(ans)


n = int(input())
a = [list(map(int, input().split())) for i in range(2)]
if n != 1:
	(sum_up, pref_up, sum_down, pref_down) = [[[0 for i in range(n)] for j in range(2)] for _ in range(4)]
	for i in range(2):
		sum_up[i][n - 1] = a[i][n - 1]
		pref_up[i][n - 1] = a[i][n - 1]
		pref_down[i][n - 1] = a[i][n - 1]
		for j in range(n - 2, -1, -1):
			sum_up[i][j] = sum_up[i][j + 1] + a[i][j]
			pref_up[i][j] = pref_up[i][j + 1] + sum_up[i][j]
			pref_down[i][j] = pref_down[i][j + 1] + a[i][j] * (n - j)
	zig = [[0 for i in range(n)] for j in range(2)]
	for j in range(n):
		for i in range(2):
			if j % 2 == 0:
				if i == 0:
					zig[i][j] = a[i][j] * j * 2 + zig[i][j - 1]
				else:
					zig[i][j] = a[i][j] * (j * 2 + 1) + zig[1 - i][j]
			elif i == 0:
				zig[1 - i][j] = a[1 - i][j] * j * 2 + zig[1 - i][j - 1]
			else:
				zig[1 - i][j] = a[1 - i][j] * (j * 2 + 1) + zig[i][j]
	ans = -1e+18
	for j in range(n):
		if j == 0:
			ans = max(ans, pref_up[0][j] + pref_down[1][j] + sum_up[1][j] * n - sum_up[0][j] - sum_up[1][j])
		elif j == n - 1:
			ans = max(ans, zig[1 - j % 2][n - 1])
		elif j % 2 == 1:
			ans = max(ans, pref_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2 + 1) - 1) + pref_down[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2 + 1) - 1 + n - j) + zig[0][j])
		else:
			ans = max(ans, pref_up[1][j + 1] - sum_up[1][j + 1] + sum_up[1][j + 1] * (4 * (j // 2) + 2) + pref_down[0][j + 1] - sum_up[0][j + 1] + sum_up[0][j + 1] * (4 * (j // 2) + 2 - 1 + n - j) + zig[1][j])
	ans = max(ans, pref_up[1][0] + pref_down[0][1] + sum_up[0][1] * n)
	print(ans)
else:
	print(a[1][0])


from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans1 = sum((a[i] * i for i in range(n))) + sum((b[-i - 1] * (n + i) for i in range(n)))
ans2 = a[0] + sum((b[i] * (i + 1) for i in range(n))) + sum((a[-i - 1] * (n + 1 + i) for i in range(n - 1)))
N = 2 * n
right = [0] * n
if n == 1:
	print(b[0])
	return
if n % 2:
	s = a[-1] + b[-1]
	right[-1] = (N - 1) * b[-1] + (N - 2) * a[-1]
	for i in range(n - 3, -1, -2):
		c = (n - i) * 2
		right[i] = right[i + 2] - 2 * s + (N - 1) * b[i] + (N - 2) * b[i + 1] + (N - c + 1) * a[i + 1] + (N - c) * a[i]
		s += a[i] + a[i + 1] + b[i] + b[i + 1]
	s = a[-1] + b[-1] + a[-2] + b[-2]
	right[-2] = (N - 1) * a[-2] + (N - 2) * a[-1] + (N - 3) * b[-1] + (N - 4) * b[-2]
	for i in range(n - 4, -1, -2):
		c = (n - i) * 2
		right[i] = right[i + 2] - 2 * s + (N - 1) * a[i] + (N - 2) * a[i + 1] + (N - c + 1) * b[i + 1] + (N - c) * b[i]
		s += a[i] + a[i + 1] + b[i] + b[i + 1]
else:
	s = a[-1] + b[-1]
	right[-1] = (N - 1) * a[-1] + (N - 2) * b[-1]
	for i in range(n - 3, -1, -2):
		c = (n - i) * 2
		right[i] = right[i + 2] - 2 * s + (N - 1) * a[i] + (N - 2) * a[i + 1] + (N - c + 1) * b[i + 1] + (N - c) * b[i]
		s += a[i] + a[i + 1] + b[i] + b[i + 1]
	s = a[-1] + b[-1] + a[-2] + b[-2]
	right[-2] = (N - 1) * b[-2] + (N - 2) * b[-1] + (N - 3) * a[-1] + (N - 4) * a[-2]
	for i in range(n - 4, -1, -2):
		c = (n - i) * 2
		right[i] = right[i + 2] - 2 * s + (N - 1) * b[i] + (N - 2) * b[i + 1] + (N - c + 1) * a[i + 1] + (N - c) * a[i]
		s += a[i] + a[i + 1] + b[i] + b[i + 1]
temp = [(a[i] * (2 * i) + b[i] * (2 * i + 1), a[i] * (2 * i + 1) + b[i] * (2 * i))[i % 2] for i in range(n)]
left = [0] + list(accumulate(temp))
right += [0]
for_ans = [left[i] + right[i] for i in range(n + 1)]
print(max(for_ans))


def getss(a):
	n = len(a)
	ps = [0] * (n + 1)
	for i in range(n - 1, 0, -1):
		ps[i] = ps[i + 1] + a[i]
	return ps

def getSum(a, b):
	n = len(a)
	ans = 0
	for i in range(n):
		ans += a[i] * i
	for i in range(n, 2 * n):
		ans += b[2 * n - i - 1] * i
	return ans

def main():
	(n,) = map(int, input().split(' '))
	a = [x for x in map(int, input().split(' '))]
	b = [x for x in map(int, input().split(' '))]
	if n == 1:
		print(b[0])
		return
	ssa = getss(a)
	ssb = getss(b)
	tans = getSum(a, b)
	ans = tans
	for i in range(0, n - 1, 2):
		tans -= i * 2 * a[i] + (i * 2 + 1) * a[i + 1] + (2 * n - 1) * b[i] + (2 * n - 2) * b[i + 1]
		tans += i * 2 * a[i] + (i * 2 + 3) * a[i + 1] + (i * 2 + 1) * b[i] + (i * 2 + 2) * b[i + 1]
		tans += 2 * ssa[i + 2] + 2 * ssb[i + 2]
		ans = max(ans, tans)
	tans = getSum(b, a)
	tans -= (2 * n - 1) * a[0]
	tans += b[0]
	tans += ssa[1] + ssb[1]
	ans = max(ans, tans)
	for i in range(1, n - 1, 2):
		tans -= i * 2 * b[i] + (i * 2 + 1) * b[i + 1] + (2 * n - 1) * a[i] + (2 * n - 2) * a[i + 1]
		tans += i * 2 * b[i] + (i * 2 + 3) * b[i + 1] + (i * 2 + 1) * a[i] + (i * 2 + 2) * a[i + 1]
		tans += 2 * ssa[i + 2] + 2 * ssb[i + 2]
		ans = max(ans, tans)
	print(ans)
main()


n = int(input())
u1 = list(map(int, input().split()))
u2 = list(map(int, input().split()))
a1 = u1[:n]
a2 = u2[:n]
for i in range(1, n):
	a1[i] += a1[i - 1]
	a2[i] += a2[i - 1]
q1 = [0] * (2 * n)
q2 = [0] * (2 * n)
for i in range(1, n):
	q1[i] = u1[i] * i + q1[i - 1]
	q2[i] = u2[i] * i + q2[i - 1]
for i in range(n):
	q1[i + n] = u2[n - i - 1] * (n + i) + q1[i + n - 1]
	q2[i + n] = u1[n - i - 1] * (n + i) + q2[i + n - 1]
ans = q1[-1]
cur = 0
for i in range(n):
	ans1 = (a1[-1] - a1[i] + a2[-1] - a2[i]) * (i + 1)
	if i % 2 == 0:
		cur += u1[i] * (i * 2)
		cur += u2[i] * (i * 2 + 1)
		ans1 += q2[n * 2 - i - 2] - q2[i] + cur
	else:
		cur += u2[i] * (i * 2)
		cur += u1[i] * (i * 2 + 1)
		ans1 += q1[n * 2 - i - 2] - q1[i] + cur
	ans = max(ans, ans1)
print(ans)


R = lambda : map(int, input().split())
n = int(input())
arr = [list(R()), list(R())]
lus = [0] * (n + 1)
lds = [0] * (n + 1)
rs = [0] * (n + 1)
for i in range(n):
	lus[i] = i * arr[0][i] + (2 * n - 1 - i) * arr[1][i]
	lds[i] = i * arr[1][i] + (2 * n - 1 - i) * arr[0][i]
for i in range(n - 1, -1, -1):
	lus[i] += lus[i + 1]
	lds[i] += lds[i + 1]
	rs[i] = rs[i + 1] + arr[0][i] + arr[1][i]
dp = [0] * (n + 1)
for i in range(0, n, 2):
	dp[i] = lus[i] + i * rs[i]
for i in range(1, n, 2):
	dp[i] = lds[i] + i * rs[i]
res = dp[0]
acc = 0
for i in range(n):
	if i % 2 == 0:
		acc += arr[0][i] * (2 * i) + arr[1][i] * (2 * i + 1)
	else:
		acc += arr[1][i] * (2 * i) + arr[0][i] * (2 * i + 1)
	res = max(res, acc + dp[i + 1])
print(res)


def main():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	roundup = []
	rounddown = []
	sumsofar = []
	for (i, (a, b)) in enumerate(zip(reversed(A), reversed(B))):
		ssf = sumsofar[-1] if sumsofar else 0
		t = roundup[-1] if roundup else 0
		roundup.append(t + ssf + b * (1 + 2 * i))
		t = rounddown[-1] if rounddown else 0
		rounddown.append(t + ssf + a * (1 + 2 * i))
		sumsofar.append(ssf + a + b)
	roundup = list(reversed(roundup))
	rounddown = list(reversed(rounddown))
	sumsofar = list(reversed(sumsofar))
	bestres = 0
	sofar = 0
	for (i, (a, b)) in enumerate(zip(A, B)):
		(rup, rdo) = (roundup, rounddown)
		if i % 2:
			(a, b) = (b, a)
			(rup, rdo) = (rdo, rup)
		bestres = max(bestres, sofar + rup[i] + sumsofar[i] * 2 * i)
		sofar += a * 2 * i
		sofar += b * (2 * i + 1)
		bestres = max(bestres, sofar)
	print(bestres)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
curl_ud_weights = [0] * n
curl_du_weights = [0] * n
sums = [0] * n
sums[n - 1] = a[n - 1] + b[n - 1]
curl_ud_weights[n - 1] = b[n - 1]
curl_du_weights[n - 1] = a[n - 1]
for i in range(n - 2, -1, -1):
	sums[i] = sums[i + 1] + (a[i] + b[i])
	remain = n * 2 - i * 2 - 1
	curl_ud_weights[i] = sums[i + 1] + curl_ud_weights[i + 1] + remain * b[i]
	curl_du_weights[i] = sums[i + 1] + curl_du_weights[i + 1] + remain * a[i]
snake_weight = 0
max_weight = 0
current_weight = -1
for t in range(0, 2 * n, 2):
	remain = t % 4
	if remain == 0:
		current_weight = sums[t // 2] * t + curl_ud_weights[t // 2] + snake_weight
		snake_weight += a[t // 2] * t + b[t // 2] * (t + 1)
	elif remain == 2:
		current_weight = sums[t // 2] * t + curl_du_weights[t // 2] + snake_weight
		snake_weight += b[t // 2] * t + a[t // 2] * (t + 1)
	if current_weight > max_weight:
		max_weight = current_weight
print(max_weight)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = [0] * (n + 1)
time = 0
for i in range(n):
	if i % 2 == 0:
		tmp = time * a[i] + (time + 1) * b[i]
	else:
		tmp = (time + 1) * a[i] + time * b[i]
	ans[i + 1] = tmp + ans[i]
	time += 2
ab = a + b[::-1]
ba = b + a[::-1]
ruisekiab = [0] * (len(ab) + 1)
ruisekiba = [0] * (len(ba) + 1)
ruisekiab2 = [0] * (len(ab) + 1)
ruisekiba2 = [0] * (len(ba) + 1)
for i in range(len(ab)):
	ruisekiab[i + 1] = ruisekiab[i] + ab[i]
	ruisekiba[i + 1] = ruisekiba[i] + ba[i]
	ruisekiab2[i + 1] = ruisekiab2[i] + ab[i] * i
	ruisekiba2[i + 1] = ruisekiba2[i] + ba[i] * i
for i in range(n + 1):
	if i % 2 == 0:
		ans[i] += (ruisekiab[2 * n - i] - ruisekiab[i]) * i
		ans[i] += ruisekiab2[2 * n - i] - ruisekiab2[i]
	else:
		ans[i] += (ruisekiba[2 * n - i] - ruisekiba[i]) * i
		ans[i] += ruisekiba2[2 * n - i] - ruisekiba2[i]
print(max(ans))


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa1 = []
sa2 = []
sa3 = []
j = 0
s = 0
while j < n:
	s += a[j] * j
	j += 1
j = 0
while j < n:
	sa1.append(s)
	s -= a[j] * j
	j += 1
s = 0
j = 0
while j < n:
	s += a[j] * (n - j)
	j += 1
j = 0
while j < n:
	sa2.append(s)
	s -= a[j] * (n - j)
	j += 1
j = 0
s = 0
while j < n:
	s += a[j]
	j += 1
j = 0
while j < n:
	sa3.append(s)
	s -= a[j]
	j += 1
sb1 = []
sb2 = []
sb3 = []
j = 0
s = 0
while j < n:
	s += b[j] * j
	j += 1
j = 0
while j < n:
	sb1.append(s)
	s -= b[j] * j
	j += 1
s = 0
j = 0
while j < n:
	s += b[j] * (n - j)
	j += 1
j = 0
p = 0
while j < n:
	sb2.append(s)
	s -= b[j] * (n - j)
	j += 1
j = 0
s = 0
while j < n:
	s += b[j]
	j += 1
j = 0
while j < n:
	sb3.append(s)
	s -= b[j]
	j += 1
ans = -1
ans = max(ans, sa1[0] + sb2[0] + (n - 1) * sb3[0])
i = 0
s = 0
p = 1
while i < n - 1:
	if i % 2 == 0:
		s += p * b[i]
		ans = max(ans, s + sb1[i + 1] + (p - i) * sb3[i + 1] + sa2[i + 1] + (p + n - 1 - i) * sa3[i + 1])
		s += (p + 1) * b[i + 1]
		p += 2
	else:
		s += p * a[i]
		ans = max(ans, s + sa1[i + 1] + (p - i) * sa3[i + 1] + sb2[i + 1] + (p + n - 1 - i) * sb3[i + 1])
		s += (p + 1) * a[i + 1]
		p += 2
	i += 1
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
v = [[], []]
v[0] = list(map(int, input().split()))
v[1] = list(map(int, input().split()))
dp = [0 for _ in range(n)]
dp1 = dp[:]
sum = v[0][n - 1] + v[1][n - 1]
dp[n - 1] = sum + v[0][n - 1]
dp1[n - 1] = sum + v[1][n - 1]
for i in range(n - 2, -1, -1):
	xx = v[1][i] + 2 * (n - i) * v[0][i] + sum
	dp[i] = xx + dp[i + 1]
	xx = v[0][i] + 2 * (n - i) * v[1][i] + sum
	dp1[i] = xx + dp1[i + 1]
	sum += v[1][i] + v[0][i]
mxmsum = sum
mxm = 0
sum = 0
turn = 0
time = 0
for i in range(0, n - 1):
	if turn == 0:
		mxm = max(mxm, sum + time * mxmsum + dp1[i + 1] + (2 * (n - i) - 1) * v[1][i])
		sum += time * v[0][i] + (time + 1) * v[1][i]
	else:
		mxm = max(mxm, sum + time * mxmsum + dp[i + 1] + (2 * (n - i) - 1) * v[0][i])
		sum += time * v[1][i] + (time + 1) * v[0][i]
	time += 2
	turn ^= 1
	mxmsum -= v[1][i] + v[0][i]
if turn:
	sum += time * v[1][n - 1] + (time + 1) * v[0][n - 1]
else:
	sum += time * v[0][n - 1] + (time + 1) * v[1][n - 1]
print(max(mxm, sum))


from sys import stdin, stdout
import heapq
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
import sys
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warm_up = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7

class MergeFind:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def prime_factors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def is_prime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	nonlocal factorial_modP, fac_warm_up
	if fac_warm_up:
		return
	factorial_modP = [1 for _ in range(fac_warm_up_size + 1)]
	for i in range(2, fac_warm_up_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warm_up = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	nonlocal fac_warm_up, factorial_modP
	if not fac_warm_up:
		warm_up_fac(MOD)
		fac_warm_up = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return list(map(int, stdin.readline().split()))

def get_list():
	return list(map(int, stdin.readline().split()))
memory = dict()

def clear_cache():
	nonlocal memory
	memory = dict()

def cached_fn(fn, *args):
	nonlocal memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_search(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
optimise_for_recursion = True

def main():
	n = get_int()
	li1 = get_list()
	li2 = get_list()
	processed = [0 for _ in range(n + 1)]
	sums = [0 for _ in range(n + 1)]
	for i in range(n - 1, -1, -1):
		sums[i] = sums[i + 1] + li1[i] + li2[i]
	if n % 2 == 0:
		processed[n - 1] = li1[n - 1]
	else:
		processed[n - 1] = li2[n - 1]
	for i in range(n - 3 if n % 2 == 1 else n - 2, -1, -2):
		k = 2 * (n - i - 1)
		processed[i] = li1[i + 1] + processed[i + 2] + 2 * sums[i + 2] + k * li2[i + 1] + (k + 1) * li2[i]
	for i in range(n - 3 if n % 2 == 0 else n - 2, -1, -2):
		k = 2 * (n - i - 1)
		processed[i] = li2[i + 1] + processed[i + 2] + 2 * sums[i + 2] + k * li1[i + 1] + (k + 1) * li1[i]
	mul = 0
	curr_prefix = 0
	res = 0
	for i in range(n):
		res = max(res, processed[i] + sums[i] * 2 * i + curr_prefix)
		if i % 2 == 0:
			curr_prefix += li1[i] * mul + li2[i] * (mul + 1)
		else:
			curr_prefix += li2[i] * mul + li1[i] * (mul + 1)
		mul += 2
	res = max(res, curr_prefix)
	print(res)
if TestCases:
	for i in range(get_int()):
		main()
else:
	main() if not optimise_for_recursion else threading.Thread(target=main).start()


n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
prefa = [int(0) for i in range(n)]
prefb = [int(0) for i in range(n)]
prefa1 = [int(0) for i in range(n)]
prefb1 = [int(0) for i in range(n)]
prefa2 = [int(0) for i in range(n)]
prefb2 = [int(0) for i in range(n)]
(rs, bufsum, bufsum1) = (0, 0, 0)
a[0] = 0
prefa[n - 1] = (n - 1) * a[n - 1]
prefa1[n - 1] = a[n - 1]
prefb[n - 1] = n * b[n - 1]
prefb1[n - 1] = b[n - 1]
prefa2[n - 1] = n * a[n - 1]
prefb2[n - 1] = (n - 1) * b[n - 1]
for i in range(n - 2, -1, -1):
	prefa[i] = prefa[i + 1] + a[i] * i
	prefa1[i] = prefa1[i + 1] + a[i]
	prefb[i] = prefb[i + 1] + b[i] * (n + n - i - 1)
	prefb1[i] = prefb1[i + 1] + b[i]
	prefa2[i] = prefa2[i + 1] + a[i] * (n + n - i - 1)
	prefb2[i] = prefb2[i + 1] + b[i] * i
for i in range(n):
	if i % 2 == 0:
		bufsum1 = prefa[i] + prefb[i] + prefa1[i] * i + prefb1[i] * i
	else:
		bufsum1 = prefa2[i] + prefb2[i] + prefa1[i] * i + prefb1[i] * i
	rs = max(bufsum + bufsum1, rs)
	if i % 2 == 0:
		bufsum += 2 * i * a[i] + (2 * i + 1) * b[i]
	else:
		bufsum += 2 * i * b[i] + (2 * i + 1) * a[i]
rs = max(rs, bufsum)
print(rs)


n = int(input())
F = list(map(int, input().split()))
S = list(map(int, input().split()))
SUM = [0] * n
SUM[n - 1] = F[n - 1] + S[n - 1]
for i in range(n - 2, -1, -1):
	SUM[i] = SUM[i + 1] + F[i] + S[i]
ANS1 = 0
for i in range(n):
	ANS1 += F[i] * i
	ANS1 += S[i] * (2 * n - 1 - i)
ANS2 = 0
for i in range(n):
	ANS2 += S[i] * (i + 1)
for i in range(1, n):
	ANS2 += F[i] * (2 * n - i)
x = ANS1
y = ANS2
ANS = [x, y]
for i in range(0, max(0, n - 2), 2):
	x = x + F[i + 1] * 2 - S[i] * (2 * n - 2 - i - i) - S[i + 1] * (2 * n - 4 - i - i) + SUM[i + 2] * 2
	y = y - F[i + 1] * (2 * n - i - i - 4) - F[i + 2] * (2 * n - i - i - 4) + SUM[i + 2] * 2
	ANS.append(x)
	ANS.append(y)
print(max(ANS))


def main():
	n = int(input())
	N = n + 1
	row1 = [int(c) for c in input().split()]
	row2 = [int(c) for c in input().split()]
	grid = [row1, row2]
	sum123 = [[0] * N, [0] * N]
	sum321 = [[0] * N, [0] * N]
	sum111 = [[0] * N, [0] * N]
	for i in range(2):
		for j in range(n - 1, -1, -1):
			sum123[i][j] = sum123[i][j + 1] + (j + 1) * grid[i][j]
			sum321[i][j] = sum321[i][j + 1] + (n - j) * grid[i][j]
			sum111[i][j] = sum111[i][j + 1] + grid[i][j]
	res = _sum = i = 0
	for j in range(n):
		nres = _sum
		nres += sum123[i][j] + j * sum111[i][j]
		nres += sum321[i ^ 1][j] + (j + n) * sum111[i ^ 1][j]
		res = max(res, nres)
		_sum += grid[i][j] * (j + j + 1)
		_sum += grid[i ^ 1][j] * (j + j + 2)
		i ^= 1
	for j in range(n):
		res -= grid[0][j] + grid[1][j]
	print(res)
main()


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

def ti():
	return list(map(int, input().split()))

def ts():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
mod = 998244353
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	C()
dp = []

def D():
	[n, k] = ti()
	a = ti()
	a = sorted(a)
	cnt = [0 for i in range(n)]
	for i in range(n):
		c = 0
		for j in range(i, n):
			if a[j] - a[i] <= 5:
				c += 1
			else:
				break
		cnt[i] = c
	global dp
	dp = [[0 for j in range(k + 1)] for i in range(n + 1)]
	ans = 0
	for i in range(n):
		for j in range(k + 1):
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
			if j + 1 <= k:
				dp[i + cnt[i]][j + 1] = max(dp[i + cnt[i]][j + 1], dp[i][j] + cnt[i])
	print(dp[n][k])

def C():
	n = pi()
	a = ti()
	b = ti()
	(sumA123, sumB123, sumA321, sumB321, spSum) = ([0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)], [0 for i in range(n)])
	smA = [0 for i in range(n)]
	smB = [0 for i in range(n)]
	for i in range(n):
		if i == 0:
			sumA123[i] = 0
			sumB123[i] = 0
			continue
		sumA123[i] = sumA123[i - 1] + i * a[i]
		sumB123[i] = sumB123[i - 1] + i * b[i]
	for i in range(n - 1, -1, -1):
		if i == n - 1:
			sumA321[i] = a[i]
			sumB321[i] = b[i]
			continue
		sumA321[i] = sumA321[i + 1] + (n - i) * a[i]
		sumB321[i] = sumB321[i + 1] + (n - i) * b[i]
	for i in range(n):
		if i == 0:
			spSum[i] = b[i]
		elif i % 2 == 0:
			spSum[i] = spSum[i - 1] + 2 * i * a[i] + (2 * i + 1) * b[i]
		else:
			spSum[i] = spSum[i - 1] + (2 * i + 1) * a[i] + 2 * i * b[i]
	for i in range(n):
		if i == 0:
			smA[i] = a[i]
			smB[i] = b[i]
			continue
		smA[i] = smA[i - 1] + a[i]
		smB[i] = smB[i - 1] + b[i]
	mx = 0
	for i in range(n):
		if i == 0:
			mx = sumA123[n - 1] + sumB321[i] + (2 * i + n - 1 - i) * smB[n - 1]
			continue
		if i % 2 == 0:
			mx = max(mx, sumA123[n - 1] - sumA123[i - 1] + (2 * i - i) * (smA[n - 1] - smA[i - 1]) + sumB321[i] + (2 * i + n - 1 - i) * (smB[n - 1] - smB[i - 1]) + spSum[i - 1])
		else:
			mx = max(mx, sumB123[n - 1] - sumB123[i - 1] + (2 * i - i) * (smB[n - 1] - smB[i - 1]) + sumA321[i] + (2 * i + n - 1 - i) * (smA[n - 1] - smA[i - 1]) + spSum[i - 1])
	print(mx)
main()


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = 0
sumTop = [0] * n
s = 0
for i in range(n - 1, -1, -1):
	s += a[i]
	sumTop[i] = s
s = 0
sumBottom = [0] * n
for i in range(n - 1, -1, -1):
	s += b[i]
	sumBottom[i] = s
dpTop = [0] * n
s = 0
for i in range(n):
	s += a[i] * i
	s += b[i] * (2 * n - i - 1)
dpTop[0] = s
dpBottom = [0] * n
s = 0
for i in range(n):
	s += b[i] * i
	s += a[i] * (2 * n - i - 1)
dpBottom[0] = s
for i in range(1, n):
	dpTop[i] = dpTop[i - 1] - sumTop[i] - sumBottom[i] - b[i - 1] * (2 * n - (i - 1) * 2 - 1)
for i in range(1, n):
	dpBottom[i] = dpBottom[i - 1] - sumTop[i] - sumBottom[i] - a[i - 1] * (2 * n - (i - 1) * 2 - 1)
dpTop.append(0)
dpBottom.append(0)
sumTop.append(0)
sumBottom.append(0)
s = 0
ans = 0
for i in range(n):
	if i % 2 == 0:
		ans = max(ans, s + dpTop[i] + 2 * i * (sumTop[i] + sumBottom[i]))
		s += a[i] * (2 * i)
		s += b[i] * (2 * i + 1)
	else:
		ans = max(ans, s + dpBottom[i] + 2 * i * (sumTop[i] + sumBottom[i]))
		s += a[i] * (2 * i + 1)
		s += b[i] * (2 * i)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
upper_lst = [0 for i in range(n + 1)]
lower_lst = [0 for i in range(n + 1)]
(count_u, count_l) = (0, 0)
count = 1
pre_total = 0
upper_sum = a[:]
lower_sum = b[:]
for i in range(n - 2, -1, -1):
	upper_sum[i] += upper_sum[i + 1]
	lower_sum[i] += lower_sum[i + 1]
for i in range(n - 1, -1, -1):
	upper_lst[i] = b[i] * count + pre_total + upper_lst[i + 1]
	lower_lst[i] = a[i] * count + pre_total + lower_lst[i + 1]
	pre_total += a[i] + b[i]
	count += 2
maxi = 0
total = 0
count = 0
for i in range(n):
	if i % 2 == 0:
		maxi = max(maxi, upper_lst[i] + total + count * (upper_sum[i] + lower_sum[i]))
		total += a[i] * count + b[i] * (count + 1)
	else:
		maxi = max(maxi, lower_lst[i] + total + count * (upper_sum[i] + lower_sum[i]))
		total += b[i] * count + a[i] * (count + 1)
	count += 2
print(maxi)


n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
sum = [0] * n
sum[-1] = a[-1] + b[-1]
for i in range(n - 2, -1, -1):
	sum[i] = a[i] + b[i] + sum[i + 1]
da = [0] * n
db = [0] * n
da[-1] = b[-1]
db[-1] = a[-1]
for i in range(n - 2, -1, -1):
	da[i] = sum[i + 1] + da[i + 1] + ((n - i) * 2 - 1) * b[i]
	db[i] = sum[i + 1] + db[i + 1] + ((n - i) * 2 - 1) * a[i]
suml = 0
ans = [0] * n
for i in range(0, n):
	if i % 2 == 0:
		ans[i] = suml + da[i] + 2 * i * sum[i]
		suml += 2 * i * a[i] + (2 * i + 1) * b[i]
	else:
		ans[i] = suml + db[i] + 2 * i * sum[i]
		suml += 2 * i * b[i] + (2 * i + 1) * a[i]
print(max(ans))


import sys
from array import array

def readline():
	return sys.stdin.buffer.readline().decode('utf-8')
n = int(readline())
a = [list(map(int, readline().split())) for _ in range(2)]
acc_m1 = [0] * (2 * n + 1)
acc1 = [0] * (2 * n + 1)
acc_m2 = [0] * (2 * n + 1)
acc2 = [0] * (2 * n + 1)
for i in range(n):
	acc_m1[i + 1] = acc_m1[i] + a[0][i] * i
	acc1[i + 1] = acc1[i] + a[0][i]
	acc_m2[i + 1] = acc_m2[i] + a[1][i] * i
	acc2[i + 1] = acc2[i] + a[1][i]
for (i, j) in zip(range(n - 1, -1, -1), range(n, 2 * n)):
	acc_m1[j + 1] = acc_m1[j] + a[1][i] * j
	acc1[j + 1] = acc1[j] + a[1][i]
	acc_m2[j + 1] = acc_m2[j] + a[0][i] * j
	acc2[j + 1] = acc2[j] + a[0][i]
x = 0
ans = 0
for i in range(n):
	if i & 1 == 0:
		ans = max(ans, x + acc_m1[2 * n - i] - acc_m1[i] + (acc1[2 * n - i] - acc1[i]) * i)
		x += a[0][i] * (i * 2) + a[1][i] * (i * 2 + 1)
	else:
		ans = max(ans, x + acc_m2[2 * n - i] - acc_m2[i] + (acc2[2 * n - i] - acc2[i]) * i)
		x += a[1][i] * (i * 2) + a[0][i] * (i * 2 + 1)
print(ans)


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
dp = [0] * (n // 2 + 1)
dp2 = [0] * (n // 2 + 1)
fi = 0
se = 0
for i in range(n):
	fi += a[i] * i
	fi += b[-i - 1] * (i + n)
dp[0] = fi
for i in range(n):
	if i != 0:
		se += a[-i] * (i + n)
	se += b[i] * (i + 1)
dp2[0] = se
gou = sum(a) + sum(b) - a[0]
gou2 = gou + 0
for i in range(1, n // 2 + 1):
	gou -= b[i * 2 - 2] + b[i * 2 - 1]
	dp[i] = dp[i - 1] - b[i * 2 - 2] * (2 * n - 4 * (i - 1) - 2) - b[i * 2 - 1] * (2 * n - 4 * (i - 1) - 4) + gou * 2
	if i == n // 2:
		break
	gou -= a[2 * i] + a[2 * i - 1]
for i in range(1, n // 2 + 1):
	try:
		gou2 -= a[2 * i] + a[2 * i - 1] + b[2 * i - 2] + b[2 * i - 1]
	except:
		break
	dp2[i] = dp2[i - 1] - a[i * 2 - 1] * (2 * n - 4 * (i - 1) - 4) - a[i * 2] * (2 * n - 4 * (i - 1) - 6) + gou2 * 2
dp.extend(dp2)
print(max(dp))


import sys
import math
n = int(sys.stdin.readline())
arrup = list(map(int, sys.stdin.readline().split()))
arrd = list(map(int, sys.stdin.readline().split()))
preup = [0]
pred = [0]
s = 0
cnt = 0
for i in range(n):
	s += arrup[i]
	cnt += arrd[i]
	preup.append(s)
	pred.append(cnt)
dp = [[0, 0] for x in range(n)]
dp[n - 1][0] = arrd[n - 1]
dp[n - 1][1] = arrup[n - 1]
for i in range(n - 2, -1, -1):
	(x, y) = (preup[n] - preup[i + 1], pred[n] - pred[i + 1])
	dp[i][0] = dp[i + 1][0] + x + y + arrd[i] * (2 * (n - i) - 1)
	dp[i][1] = dp[i + 1][1] + x + y + arrup[i] * (2 * (n - i) - 1)
s = 0
t = 0
ans = dp[0][0]
for i in range(n - 1):
	if i % 2 == 0:
		s += t * arrup[i]
		t += 1
		s += t * arrd[i]
		t += 1
		(c, d) = (preup[n] - preup[i + 1], pred[n] - pred[i + 1])
		cost = s + dp[i + 1][1] + c * t + d * t
		ans = max(ans, cost)
	else:
		s += t * arrd[i]
		t += 1
		s += t * arrup[i]
		t += 1
		(c, d) = (preup[n] - preup[i + 1], pred[n] - pred[i + 1])
		cost = s + dp[i + 1][0] + c * t + d * t
		ans = max(ans, cost)
print(ans)


def main():
	[n] = list(map(int, input().split(' ')))
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	sums = []
	for i in range(n):
		if len(sums) == 0:
			sums.append(a[-1] + b[-1])
		else:
			sums.append(sums[-1] + a[-i - 1] + b[-i - 1])
	bottom_to_top = []
	top_to_bottom = []
	for i in range(n):
		if i == 0:
			bottom_to_top.append(a[-1])
			top_to_bottom.append(b[-1])
		else:
			bottom_to_top.append(bottom_to_top[-1] + sums[i - 1] + a[-i - 1] * (2 * i + 1))
			top_to_bottom.append(top_to_bottom[-1] + sums[i - 1] + b[-i - 1] * (2 * i + 1))
	best_t = []
	best_b = []
	for i in range(n):
		if i == 0:
			best_t.append(top_to_bottom[0])
			best_b.append(bottom_to_top[0])
		else:
			x = b[-i - 1] + 2 * sums[i - 1] + best_b[i - 1]
			y = a[-i - 1] + 2 * sums[i - 1] + best_t[i - 1]
			best_t.append(max([x, top_to_bottom[i]]))
			best_b.append(max([y, bottom_to_top[i]]))
	print(best_t[-1])
main()


from sys import stdin
from math import *
line = stdin.readline().rstrip().split()
n = int(line[0])
numbers = list(map(int, stdin.readline().rstrip().split()))
numbers2 = list(map(int, stdin.readline().rstrip().split()))
growRateAccum = 0
topAccum = 0
bottomAccum = 0
accumsB = [0] * n
accumsT = [0] * n
growRateAccum = 0
accum = 0
for i in range(n - 1, -1, -1):
	accum += numbers[i] * ((n - i) * 2 - 1 - 1)
	growRateAccum += numbers[i]
	if i < n - 1:
		growRateAccum += numbers2[i + 1]
	accum += growRateAccum
	accumsB[i] = accum
growRateAccum = 0
accum = 0
for i in range(n - 1, -1, -1):
	accum += numbers2[i] * ((n - i) * 2 - 1 - 1)
	growRateAccum += numbers2[i]
	if i < n - 1:
		growRateAccum += numbers[i + 1]
	accum += growRateAccum
	accumsT[i] = accum
cMax = 0
currAccum = 0
growRateAccum = sum(numbers[1:]) + sum(numbers2[1:])
for i in range(n):
	if i % 2 == 0:
		cMax = max(cMax, currAccum + accumsT[i])
	else:
		cMax = max(cMax, currAccum + accumsB[i])
	if i < n - 1:
		if i % 2 == 0:
			currAccum += numbers2[i] + growRateAccum * 2
		else:
			currAccum += numbers[i] + growRateAccum * 2
		growRateAccum -= numbers[i + 1]
		growRateAccum -= numbers2[i + 1]
print(cMax)


n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
from itertools import accumulate
CA = [0] + A
CA = list(accumulate(CA))
CB = [0] + B
CB = list(accumulate(CB))
A1 = [0] * n
B1 = [0] * n
for j in range(n):
	A1[j] = A[j] * j
	B1[j] = B[j] * (2 * n - 1 - j)
A2 = [0] * n
B2 = [0] * n
for j in range(n):
	A2[j] = A[j] * (2 * n - 1 - j)
	B2[j] = B[j] * j
C1 = [0] * (n + 1)
C2 = [0] * (n + 1)
for j in range(1, n + 1):
	C1[j] += C1[j - 1]
	C1[j] += A1[j - 1] + B1[j - 1]
	C2[j] += C2[j - 1]
	C2[j] += A2[j - 1] + B2[j - 1]
INF = 10 ** 18
ans = -INF
t = 0
for i in range(n + 1):
	if i % 2 == 0:
		temp = t + C1[-1] - C1[i] + (CA[-1] - CA[i]) * i + (CB[-1] - CB[i]) * i
	else:
		temp = t + C2[-1] - C2[i] + (CA[-1] - CA[i]) * i + (CB[-1] - CB[i]) * i
	ans = max(ans, temp)
	if i != n:
		if i % 2 == 0:
			t += A[i] * (2 * i) + B[i] * (2 * i + 1)
		else:
			t += A[i] * (2 * i + 1) + B[i] * (2 * i)
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=-10 ** 6, func=lambda a, b: max(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree1:

	def __init__(self, data, default=10 ** 6, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)
MOD = 10 ** 9 + 7

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod = 10 ** 9 + 7
omod = 998244353
prime = [True for i in range(200001)]
pp = [0] * 200001

def SieveOfEratosthenes(n=200000):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
n = int(input())
x = map(int, input().split())
a = [[], []]
k = 0
for i in x:
	a[0].append(i)
	k += i
x = map(int, input().split())
for i in x:
	a[1].append(i)
	k += i
sum = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum[i].append(sum[i][j - 1] + a[i][n - j])
sum2 = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum2[i].append(sum2[i][j - 1] + sum[i][j])
sum3 = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum3[i].append(sum3[i][j - 1] + a[i][n - j] * j)
ans = 0
now = 0
for i in range(n):
	pos = n - i
	line = i % 2
	ans = max(ans, now + sum[0][pos] * i * 2 + sum[1][pos] * i * 2 + sum2[line][pos] + sum3[1 - line][pos] + sum[1 - line][pos] * (n - i))
	now += (2 * i + 1) * a[line][i] + (2 * i + 2) * a[1 - line][i]
print(ans - k)


n = int(input())
F = list(map(int, input().split()))
S = list(map(int, input().split()))
Sum = [0] * n
Sum[n - 1] = F[n - 1] + S[n - 1]
for i in range(n - 2, -1, -1):
	Sum[i] = Sum[i + 1] + F[i] + S[i]
ans1 = 0
for i in range(n):
	ans1 += F[i] * i
	ans1 += S[i] * (2 * n - i - 1)
ans2 = 0
for i in range(n):
	ans2 += S[i] * (i + 1)
for i in range(1, n):
	ans2 += F[i] * (2 * n - i)
x = ans1
y = ans2
ans = [x, y]
for i in range(0, max(0, n - 2), 2):
	x = x + F[i + 1] * 2 - S[i] * (2 * n - 2 - 2 * i) - S[i + 1] * (2 * n - 4 - 2 * i) + Sum[i + 2] * 2
	y = y - F[i + 1] * (2 * n - 2 * i - 4) - F[i + 2] * (2 * n - 2 * i - 4) + Sum[i + 2] * 2
	ans.append(x)
	ans.append(y)
print(max(ans))


import sys
import io
stream_enable = 0
inpstream = '\n3\n1 2 3\n6 5 4\n'
if stream_enable:
	sys.stdin = io.StringIO(inpstream)
	input()

def inpmap():
	return list(map(int, input().split()))
n = int(input())
arr = [inpmap(), inpmap()]
s = [0] * n
s[-1] = arr[0][-1] + arr[1][-1]
for i in range(n - 2, -1, -1):
	s[i] = s[i + 1] + arr[0][i] + arr[1][i]
a = [0] * n
a[-1] = arr[1][-1] * 2 + arr[0][-1]
b = [0] * n
b[-1] = arr[0][-1] * 2 + arr[1][-1]
for i in range(n - 2, -1, -1):
	a[i] = arr[0][i] + a[i + 1] + s[i + 1] + arr[1][i] * (n - i) * 2
	b[i] = arr[1][i] + b[i + 1] + s[i + 1] + arr[0][i] * (n - i) * 2
dp = [0] * n
dp[-1] = arr[n % 2][-1]
for i in range(n - 2, -1, -1):
	d = i % 2
	g1 = arr[1 - d][i] + dp[i + 1] + s[i + 1] * 2
	g2 = (a, b)[d][i + 1] + arr[1 - d][i] * ((n - i) * 2 - 1)
	dp[i] = max(g1, g2)
print(dp[0])


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def f(l):
	s = sum(l)
	sume = [s for i in range(n)]
	s3 = [0 for i in range(n)]
	ts = 0
	for i in range(1, n):
		sume[i] = sume[i - 1] - l[i - 1]
		ts += i * l[i]
		s3[n - i - 1] = s3[n - i] + i * l[n - i - 1]
	s2 = [ts for i in range(n)]
	for i in range(1, n):
		s2[i] = s2[i - 1] - (i - 1) * l[i - 1]
	return (sume, s2, s3)
(a1, a2, a3) = f(a)
(b1, b2, b3) = f(b)
best = 0
(curr, t) = (0, 0)
for i in range(n):
	if i % 2 == 0:
		pot = curr + t * a1[i] + a2[i] - i * a1[i] + (t + n - i) * b1[i] + b3[i]
	else:
		pot = curr + t * b1[i] + b2[i] - i * b1[i] + (t + n - i) * a1[i] + a3[i]
	best = max(best, pot)
	if i % 2 == 0:
		curr += t * a[i] + (t + 1) * b[i]
	else:
		curr += t * b[i] + (t + 1) * a[i]
	t += 2
print(max(best, curr))


n = int(input())
x = map(int, input().split())
a = [[], []]
k = 0
for i in x:
	a[0].append(i)
	k += i
x = map(int, input().split())
for i in x:
	a[1].append(i)
	k += i
sum = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum[i].append(sum[i][j - 1] + a[i][n - j])
sum2 = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum2[i].append(sum2[i][j - 1] + sum[i][j])
sum3 = [[0], [0]]
for i in range(2):
	for j in range(1, n + 1):
		sum3[i].append(sum3[i][j - 1] + a[i][n - j] * j)
ans = 0
now = 0
for i in range(n):
	pos = n - i
	line = i % 2
	ans = max(ans, now + sum[0][pos] * i * 2 + sum[1][pos] * i * 2 + sum2[line][pos] + sum3[1 - line][pos] + sum[1 - line][pos] * (n - i))
	now += (2 * i + 1) * a[line][i] + (2 * i + 2) * a[1 - line][i]
print(ans - k)