import std.stdio, std.conv, std.array, std.string;
import std.algorithm;
import std.container, std.range;

void main() {
  auto N = readln.chomp.to!int;
  auto Pn = readln.split.to!(int[]);

  ulong sum;
  foreach(heap_size; 2..N+1) {
    auto heap = heapify(Pn[0..heap_size].dup);
    auto first = heap.front(); heap.removeFront();
    auto second = heap.front();
    heap.insert(first);
    sum += second;

    for(int i=1; i<N-heap_size+1; i++) {
      heap = heapify(Pn[i..i+heap_size].dup);
      heap.removeFront();
      auto s = heap.front();
      sum += s;
    }
  }
  writeln(sum);
}
