#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

struct Treap {
  long long value;
  long long priority;
  Treap* left;
  Treap* right;
  long long max;
  int size;
};

Treap* const emptyTreap = new Treap {
  0, LONG_LONG_MIN, NULL, NULL, 0, 0
};

long long getKth(Treap* root, int k) {
  assert(0 <= k && k < root->size);
  if (k < root->left->size)
    return getKth(root->left, k);
  else if (k == root->left->size)
    return root->value;
  else
    return getKth(root->right, k - root->left->size - 1);
}

Treap* recompute(Treap* newLeft, Treap* root, Treap* newRight) {
  root->left = newLeft;
  root->right = newRight;
  root->max = std::max({root->left->max, root->value, root->right->max});
  root->size = root->left->size + 1 + root->right->size;
  return root;
}

Treap* popLast(Treap* root) {
  assert(root != NULL && root != emptyTreap);
  if (root->right != emptyTreap)
    return recompute(root->left, root, popLast(root->right));
  else
    return root->left;
}

std::pair<Treap*, Treap*> split(Treap* root, long long value) {
  std::pair<Treap*, Treap*> answer;
  if (root == emptyTreap) {
    answer.first = emptyTreap;
    answer.second = emptyTreap;
  } else if (std::max(root->value, root->right->max) < value) {
    auto p = split(root->left, value);
    answer.first = p.first;
    answer.second = recompute(p.second, root, root->right);
  } else {
    auto p = split(root->right, value);
    answer.first = recompute(root->left, root, p.first);
    answer.second = p.second;
  }
  return answer;
}

Treap* newTreap(long long value) {
  long long priority =
      (long long)rand() << 48
    | (long long)rand() << 32
    | (long long)rand() << 16
    | (long long)rand() <<  0;
  if (priority == LONG_LONG_MIN)
    priority++;
  return new Treap {
    value, priority, emptyTreap, emptyTreap, value, 1
  };
}

Treap* join(Treap* first, Treap* second) {
  if (first == emptyTreap)
    return second;
  else if (second == emptyTreap)
    return first;
  else if (first->priority < second->priority)
    return recompute(join(first, second->left), second, second->right);
  else
    return recompute(first->left, first, join(first->right, second));
}

void dump(Treap* root, int offset = 0) {
  assert(root != NULL);
  if (root != emptyTreap) {
    dump(root->left, offset + 1);
    for (int i = 0; i < offset; i++)
      printf("  ");
    printf("%lld\t%5lld%5d%20lld\n",
      root->value, root->max, root->size, root->priority);
    dump(root->right, offset + 1);
  }
}

long long answer;

Treap* append(Treap* root, long long value) {
  return join(root, newTreap(value));
}

Treap* compressLast32(Treap* root) {
  if (root->size >= 3 && getKth(root, root->size - 3) <= getKth(root, root->size - 1)) {
    //printf("brefore:\n");
    //dump(root);
    long long last1 = getKth(root, root->size - 1);
    root = popLast(root);
    long long sum = getKth(root, root->size - 1);
    root = popLast(root);
    sum += getKth(root, root->size - 1);
    root = popLast(root);
    auto p = split(root, sum);
    p.first = append(p.first, sum);
    //printf("p.first = \n");
    //dump(p.first);
    //printf("EO p.first\n");
    p.first = compressLast32(p.first);
    p.second = append(p.second, last1);
    root = join(p.first, p.second);
    root = compressLast32(root);
    //printf("after:\n");
    //dump(root);
    answer += sum;
    //printf("+%lld\n", sum);
  }
  return root;
}

Treap* compressLast21(Treap* root) {
  long long sum = getKth(root, root->size - 1);
  root = popLast(root);
  sum += getKth(root, root->size - 1);
  root = popLast(root);
  auto p = split(root, sum);
  p.first = append(p.first, sum);
  p.first = compressLast32(p.first);
  root = join(p.first, p.second);
  //dump(root);
  answer += sum;
  //printf("+%lld\n", sum);
  return root;
}

int main() {
  int n;
  scanf("%d", &n);
  long long a[n];
  for (long long &ai : a)
    scanf("%lld", &ai);

  answer = 0;
  Treap* root = emptyTreap;
  for (long long ai : a) {
    root = append(root, ai);
    root = compressLast32(root);
    //dump(root);
    //printf("\n");
  }
  while (root->size >= 2)
    root = compressLast21(root);

  printf("%lld\n", answer);
  return 0;
}


Measured memory_bytes: 256