#include<iostream>
#include<vector>
using namespace std;
vector<long long> V;
long long Ans;
int main()
{
  int N;
  cin>>N;
  for(int i=0;i<N;i++)
  {
    long long X;
    cin>>X;
    V.push_back(X);
  }
  while(N>1)
  {
    N--;
    int p;
    long long a=20000000000000ll;
    for(int i=0;i<N;i++)
      if(V[i]+V[i+1]<a)
        a=V[i]+V[i+1],
        p=i;
    Ans+=V[p]+V[p+1];
    V[p]+=V[p+1];
    V.erase(V.begin()+p+1);
  }
  cout<<Ans<<endl;
  return 0;
}