#!/usr/bin/env python3
# -*- coding: utf-8 -*-


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

    a_ind = range(n)
    a_ind_s = sorted(a_ind, key=lambda x: a_list[x], reverse=True)

    def max_ans(a_i, s_min, s_max):
        if len(a_ind_s) <= a_i:
            return 0
        i = a_ind_s[a_i]
        a = a_list[i]

        return max(a * (s_max - i) + max_ans(a_i + 1, s_min, s_max - 1),
                   a * (i - s_min) + max_ans(a_i + 1, s_min + 1, s_max))

    r = max_ans(0, 0, n - 1)
    print(r)


if __name__ == '__main__':
    main()
