#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;

int arr[N];
int smax[N];

int main() {
	int n;
	cin>>n;

	int mxf;
	int mxs = 0;

	smax[0] = mxs;

	for(int i=0;i<n;i++)
        cin>>arr[i];

    long long sum = 0;
	for(int i=0;i<n-1;i++){
        mxf = arr[i];
        mxs = 0;
        for(int j=i+1;j<n;j++){
            if(arr[j] > mxf)
            {
                mxs = mxf;
                mxf = arr[j];
            }

            if(arr[j] < mxf && arr[j] > mxs){
                mxs = arr[j];
            }

            //cout<<mxs<<" ";
            sum += mxs;
        }
        //cout<<endl;
	}

    cout<<sum;

	return 0;
}
