def main():
    n = int(input())
    a = list(map(int, input().split()))

    ans = 0

    ax = []
    for i in range(n):
        ax.append((a[i], i))

    for _ in range(n):
        for i in range(n-1):
            if ax[i][0] * abs(ax[i][1] - (i+1)) + ax[i+1][0] * abs(ax[i+1][1] - i) > ax[i][0] * abs(ax[i][1] - i) + ax[i+1][0] * abs(ax[i+1][1] - (i+1)):
                ax[i], ax[i+1] = ax[i+1], ax[i]

    total = 0
    for i in range(n):
        total += ax[i][0] * abs(ax[i][1] - i)
        
    ans = max(ans, total)

    ax = []
    for i in range(n):
        ax.append((a[i], n-i-1))
    ax.reverse()

    for _ in range(n):
        for i in range(n-1):
            if ax[i][0] * abs(ax[i][1] - (i+1)) + ax[i+1][0] * abs(ax[i+1][1] - i) > ax[i][0] * abs(ax[i][1] - i) + ax[i+1][0] * abs(ax[i+1][1] - (i+1)):
                ax[i], ax[i+1] = ax[i+1], ax[i]

    total = 0
    for i in range(n):
        total += ax[i][0] * abs(ax[i][1] - i)

    ans = max(ans, total)

    print(ans)


if __name__ == '__main__':
    main()
