#include <bits/stdc++.h>

//#define DEBUG

#ifdef DEBUG
using std::endl;
std::ifstream cin("input.txt");
std::ofstream cout("output.txt");
#endif // DEBUG
#ifndef DEBUG
using namespace std;
#endif // DEBUG

template <class T>
void print_vect(std::vector<T> v){
    for(int i = 0 ; i < v .size(); i++) cout << v[i] << " ";
    cout << endl;
}


bool func_to_create(){
    uint32_t N ;
    std::vector<std::pair< uint64_t, uint32_t> > A;
    cin >> N;
    for(uint32_t i = 0 ; i < N ; i++){
        uint64_t a;
        cin >> a;
        A.push_back(std::pair<uint64_t, uint32_t>(a, i));
    }
    uint64_t ans = 0;
    uint32_t left = 0, right = N - 1;
    while(left <= right){
        uint64_t max_increase = 0;
        int left_or_right = 0;
        uint32_t swap_index = 0;
        for(uint32_t i = left ; i <= right ;i++){
            if( A[i].first * (A[i].second - left) > max_increase){
                max_increase = A[i].first * abs(A[i].second - left);
                left_or_right = 1;
                swap_index = i;
            }
            if( A[i].first * (right - A[i].second) > max_increase){
                max_increase =  A[i].first * abs(right - A[i].second);
                left_or_right = 0;
                swap_index = i;
            }
        }
 
        ans += max_increase;
        if(left_or_right){
            A[swap_index] = A[left];
            left++;
        }else{
            A[swap_index] = A[right];
            right--;
        }
    }
    cout << ans << endl;
}
int main()
{
    //making data IO Fast
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    /****************************/

    #ifdef DEBUG
    int TEST_CASES = 3;
    while(TEST_CASES--){
    #endif // DEBBUG

    func_to_create();

    #ifdef DEBUG
    }
    #endif // DEBUG
    
    return 0;
}
