#include<iostream>
#include<vector>
#include<array>
#include<algorithm>
int main(){
    int N=0;
    std::vector<int> p;
    std::cin>>N;
    for(int i=0;i<N;++i)
    {
        int t;
        std::cin>>t;
        p.push_back(t);
    }
    std::vector<std::vector<std::array<int,3>>>  tmp( N,std::vector<std::array<int,3>>(N,std::array<int,3>{0,0,0}) );

    for(int l=0;(l+1)<N;++l){
        tmp[l][l+1]={p[l],p[l+1]};
        std::sort(tmp[l][l+1].begin(),tmp[l][l+1].end(),std::greater<>());
        for(int r = l+2;r<N;++r){
            tmp[l][r] = tmp[l][r-1];
            tmp[l][r][2] = p[r];
            std::sort(tmp[l][r].begin(),tmp[l][r].end(),std::greater<>());
        }
    }
    int sum=0;
    for(int l=0;l<N;++l){
        for(int r=l+1;r<N;++r){
            //std::cout<<tmp[l][r][1]<<" ";
            sum+=tmp[l][r][1];
    }}
    std::cout<<sum;
}