import sys
sys.setrecursionlimit(10**6)


def i1str():
    return input()


def istrs(n=None):
    if n is None:
        return input().split()
    a = []
    for _ in range(n):
        a.append(istrs())
    return a


def i1num():
    return int(input())


def inums(n=None):
    if n is None:
        return list(map(int, input().split()))
    a = []
    for _ in range(n):
        a.append(inums())
    return a


def ostrs(l, sp=" "):
    print(sp.join(l))


def onums(l, sp=" "):
    print(sp.join(map(str, l)))


def solve(aii, index, left, right):
    res = 0
    ai, i = aii[index]
    if index == 0:
        return ai * abs(i-left)

    index -= 1
    # to leftmost
    res1 = ai * abs(i-left) + solve(aii, index, left+1, right)
    # to rightmost
    res2 = ai * abs(right-i) + solve(aii, index, left, right-1)
    return max(res1, res2)


if __name__ == "__main__":
    n = i1num()
    a = inums()
    aii = sorted((a[i], i)for i in range(n))
    print(solve(aii, n-1, 0, n-1))
