void main() {
  problem();
}

class Person {
  long kindness;
  Person left, right;

  this(long kindness) {
    this.kindness = kindness;
  }

  long calcEntry(Person left, Person right) {
    long confortness;

    if (left) confortness = left.kindness;
    if (right && confortness > right.kindness) confortness = right.kindness;

    [confortness].deb;

    return confortness;
  }

  long entry(Person left, Person right) {
    [left, right].deb;
    long confortness;

    this.left = left;
    if (left) {
      left.right = this;
      confortness = left.kindness;
    } else {
      this.left = this;
    }

    this.right = right;
    if (right) {
      right.left = this;
      if (confortness > right.kindness)confortness = right.kindness;
    } {
      this.right = this;
    }

    [this.left, this.right].deb;
    return confortness;
  }
}

void problem() {
  auto N = scan!int;
  auto A = scan!long(N).sort().reverse();

  long solve() {
    long ans;
    int i;
    Person prev;
    foreach(a; A) {
      Person person = new Person(a);

      if (i == 0) {
        person.entry(null, null);
      } else if (i == 1) {
        ans += person.entry(prev, prev);
      } else {
        long x;
        Person maxPerson;

        Person p = prev;
        while(true) {
          p.deb;
          long xx = person.calcEntry(p, p.right);
          if (xx > x) {
            x = xx;
            maxPerson = p;
          }

          p = p.right;
          if (p == prev) break;
        }
        ans += person.entry(maxPerson, maxPerson.right);
      }

      i++;
      prev = person; 
    }

    return ans;
  }

  solve().writeln;
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Queue = Tuple!(long, "from", long, "to");

// -----------------------------------------------
