object Main extends App {
  val in = scala.io.Source.stdin.getLines.toSeq
  val ls = in.map(_.split(' ').map(_.toInt).toArray)
  val N = ls(0).head
  val s = ls(1).sorted
  val min = s.head
  val max = s.last
  val span = max - min
  val diffs = (1 until s.size).map(i => s(i) - s(i-1)).sorted
  val r = diffs.takeRight(N-1).sum
  val ret = span - r
  println(ret)
}
