if __name__ == "__main__":
    n = int(input())
    a = [int(x) for x in input().split()]
    ex = [x for x in range(n)]

    s = 0
    e = n - 1
    p_max = 1
    ans = 0
    
    for j in range(n):
        for i, exi in enumerate(ex):
            p = max([exi - s, e - exi]) * a[exi]
            if p_max <= p:
                p_max = p
                p_max_arg = i
        ans += p_max
        p_max = 0
        if ex[p_max_arg] - s < e - ex[p_max_arg]:
            e -= 1
        else:
            s += 1
        ex.pop(p_max_arg)
    
    print(ans)