const chunks = [];
process.stdin.on("data", (chunk) => {
  chunks.push(chunk);
});
process.stdin.on("end", () => {
  const data = Buffer.concat(chunks).toString("utf8").trim();
  const lines = data.split("\n");
  const N = parseInt(lines[0], 10);
  const A = lines[1].split(" ").map((item) => parseInt(item, 10));
  console.log(solve(N, A));
});

// PriorityQueue (min heap by default)
class PriorityQueue {
  constructor(comparator, initialItems = []) {
    this.comparator = comparator;
    this.items = initialItems;
    this.heapify();
  }
  offer(item) {
    this.items.push(item);
    this.bubbleUp(this.items.length - 1);
  }
  peek() {
    return this.items[0];
  }
  poll() {
    if (this.isEmpty()) return null;
    if (this.items.length === 1) return this.items.pop();
    const top = this.items[0];
    this.items[0] = this.items.pop();
    this.siftDown(0);
    return top;
  }
  size() {
    return this.items.length;
  }
  isEmpty() {
    return this.items.length === 0;
  }
  // -- Private
  // Bubble up the value at index until the heap property is satisfied.
  // In min heap, it bubbles up until it reaches the root or it's bigger
  // than or equal to its parent.
  bubbleUp(index) {
    while (index > 0) {
      const parent = Math.floor((index + 1) / 2) - 1;
      const comparison = this.comparator(this.items[index], this.items[parent]);
      // index should stay if it's bigger than or equal to its parent
      // (min heap).
      if (comparison >= 0) {
        // Heap property is satisfied
        return;
      }
      this.swap(index, parent);
      index = parent;
    }
  }
  // Sift down the value at index until the heap property is satisfied.
  // In min heap, it sifts down until it reaches the bottom or it's smaller
  // than or equal to its smaller child.
  siftDown(index) {
    while (true) {
      const left = index * 2 + 1;
      const right = index * 2 + 2;
      if (left >= this.items.length) return; // index is at the bottom
      let child;
      if (right >= this.items.length) {
        child = left;
      } else {
        child =
          this.comparator(this.items[left], this.items[right]) <= 0
            ? left
            : right;
      }
      if (this.comparator(this.items[index], this.items[child]) <= 0) {
        // Heap property is satisfied
        return;
      }
      this.swap(index, child);
      index = child;
    }
  }

  // O(N) heapify
  // Sift down items from the bottom to the top.
  heapify() {
    // If the last parent has only one child (N is even, N = 2M):
    //   left = lastParent * 2 + 1 = N - 1 = 2M - 1
    //   lastParent = M - 1 = N/2 - 1
    // If the last parent has two children (N is odd, N = 2M + 1)
    //   right = lastParent * 2 + 2 = N - 1 = 2M
    //   lastParent = M - 1 = floor(N/2) - 1
    const lastParent = Math.floor(this.items.length / 2) - 1;
    for (let i = lastParent; i >= 0; i--) {
      this.siftDown(i);
    }
  }
  swap(a, b) {
    const temp = this.items[a];
    this.items[a] = this.items[b];
    this.items[b] = temp;
  }
}

class Pair {
  constructor(a, b) {
    this.min = Math.min(a, b);
    this.max = Math.max(a, b);
    this.diff = this.max - this.min;
  }
}

// Greedy algorithm...
function solve(N, A) {
  // Sort desc
  A.sort((a, b) => b - a);

  const maxHeap = new PriorityQueue((a, b) => {
    // Bigger min is better.
    if (a.min !== b.min) {
      return b.min - a.min;
    }
    // Smaller diff is better.
    return a.diff - b.diff;
  });

  let comfort = 0;
  for (const a of A) {
    if (maxHeap.isEmpty()) {
      maxHeap.offer(new Pair(a, a));
    } else {
      const top = maxHeap.poll();
      comfort += top.min;
      maxHeap.offer(new Pair(a, top.min));
      maxHeap.offer(new Pair(a, top.max));
    }
  }
  return comfort;
}


Measured memory_bytes: 90132