Problem:
Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8

Solution in PHP7 (7.0.15) (judge verdict: Runtime Error)

<?php
/**
 * Created by PhpStorm.
 * User: stmkza
 * Date: 2018/10/27
 * Time: 20:59
 */
function permutations(array $elements)
{
  if (count($elements) <= 1) {
    yield $elements;
  } else {
    foreach (permutations(array_slice($elements, 1)) as $permutation) {
      foreach (range(0, count($elements) - 1) as $i) {
        yield array_merge(
          array_slice($permutation, 0, $i),
          [$elements[0]],
          array_slice($permutation, $i)
        );
      }
    }
  }
}
$stdins = [];
$flg = false;
while(true)
{
  if (feof(STDIN))
  {
    break;
  }
  $stdin = trim(fgets(STDIN));
  if (!$flg) {
    $flg = true;
    continue;
  }
  $stdins[] = intval($stdin);
}

$res = 0;

foreach (permutations($stdins) as $ptn) {
  $tmp = getmax($ptn);
  if($tmp > $res) {
    $res = $tmp;
  }
}

echo $res;

function getmax(array $arr) {
  $t = 0;
  foreach ($arr as $pos => $e) {
    if($pos === 0) {
      continue;
    } else {
      $t += abs($e - $arr[$pos - 1]);
    }
  }
  return $t;
}