import std.stdio;
import std.algorithm;
import std.math;
import std.conv;
import std.string;

T readNum(T)(){
  return readStr.to!T;
}

T[] readNums(T)(){
  return readStr.split.to!(T[]);
}

string readStr(){
  return readln.chomp;
}

void main(){
  auto n = readNum!int;
  auto p = readNums!int;

  int m, s, ret;

  foreach(i; 0 .. n-1){
    m = max(p[i], p[i+1]);
    s = min(p[i], p[i+1]);
    ret += s;

    foreach(j; i+2 .. n){
      if(m < p[j]){
        s = m;
        m = p[j];
      } else if(s < p[j]){
        s = p[j];
      }

      ret += s;
    }
  }

  writeln(ret);
}
