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.

Difficulty: interview

Starter code:

Solutions:
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))


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


#for i in range(n-1, -1, -1):
#    if i % 2 == 0:
#        if i == n-1:
#            accums[i] = numbers2[i]
#        else:
 #           bottomAccum += numbers2[i]*((n-i)*2-1)
  #          growRateAccum += numbers2[i] + numbers[i+1]
   #         bottomAccum += growRateAccum
#        accums[i] = bottomAccum
 #   if i % 2 == 1:
  #      if i == n-1:
  #          accums[i] = numbers2[i]
  #      else:
   #         topAccum += numbers[i]*((n-i)*2-1)
   #         growRateAccum += numbers[i] + numbers2[i+1]
   #         topAccum += growRateAccum
   #         accums[i] = topAccum
cMax = 0
currAccum = 0

growRateAccum = sum(numbers[1::]) + sum(numbers2[1::])

for i in range(n):
    if i%2==0:
        #estoy en top:
        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)



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 u, d in zip(roundup, rounddown):
	# 	print(u, d, '-')

	for i, (a, b) in enumerate(zip(A, B)):
		rup, rdo = roundup, rounddown
		if i%2:
			a, b = b, a
			rup, rdo = rdo, rup
		# print(i, sofar + rup[i])
		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)

def __starting_point():
	main()
__starting_point()

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
#print(value)
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(s + value[i % 2][(i + 1)])

print(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, = list(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)

def __starting_point():
    main()

__starting_point()

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


from itertools import accumulate

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

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


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

    # print(sum)

    d_a[-1] = b[-1]
    d_b[-1] = a[-1]

    for i in reversed(list(range(n - 1))):
        # print(n-i)
        # print((n - i) * 2 - 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()


def __starting_point():
    # test(1)
    # test(2)
    return(main())

__starting_point()

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)


#SABUN[i]=F[i+1]*2+S[i]*(2*n-1-i-i)+S[i+1]*(n-3-i-i)+SUM[2*i]*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(ANS,i)

print(max(ANS))






import sys
import io

stream_enable = 0

inpstream = """
3
1 2 3
6 5 4
"""

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]
# print(s)

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
# print(a)
# print(b)

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])


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(list(reversed(best_t)))
  #print(list(reversed(best_b)))
  print(best_t[-1])
main()

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

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())
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]
            else:
                if 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 = -1e18
    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])
        else:
            if j == n - 1:
                ans = max(ans, zig[1 - (j % 2)][n - 1])
            else:
                if 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])


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

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



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

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())
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]
    #print(sums[i] + curl_ud_weights[i + 1], remain, "*", b[i])

# -----------------------------------------------------

snake_weight = 0
max_weight = 0
current_weight = -1

for t in range(0, 2 * n, 2):
    remain = t % 4
    #print(t, snake_weight, a[t//2], b[t//2])
    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, t)

print(max_weight)
#print(curl_ud_weights)
#print(curl_du_weights)
#print(sums)

"""
3
0 1 0
0 0 0
"""

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)                                            


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)

    
def __starting_point():
    main()

__starting_point()

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)

    
def __starting_point():
    main()

__starting_point()

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)

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
#print(ans)
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(ans1, cur)
print(ans)
'''
3
1 100000 10000
10 100 1000
6
12 8 12 17 20 5
17 4 8 8 8 4
'''


''' CODED WITH LOVE BY SATYAM KUMAR '''

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)  # max depth of recursion
threading.stack_size(2 ** 27)  # new thread will get stack of such size
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):  # n**0.5 complex
    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):
    """Returns True if n is prime."""
    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:  # Change this condition 2 to whatever you like
            x += b
        b = b // 2
    return x


# -------------------------------------------------------------- MAIN PROGRAM


TestCases = False
optimise_for_recursion = True  # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3


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]
    #print(processed, sums)
    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)
# --------------------------------------------------------------------- END=


if TestCases:
    for i in range(get_int()):
        main()
else:
    main() if not optimise_for_recursion else threading.Thread(target=main).start()