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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class MainClass : Scanner
{
    static void Main()
    {
        int N = RInt();
        var A = new long[N];
        for (int i = 0; i < N; i++)
        {
            A[i] = RLong();
        }
        Array.Sort(A);

        long ans = 0;
        {
            int l = 1, r = N - 1;
            long lv = A[0], rv = A[0];
            long ans1 = 0;
            while (l <= r)
            {
                var dif1 = Abs(lv - A[r]);
                var dif2 = Abs(rv - A[r]);
                if (dif1 > dif2) lv = A[r];
                else rv = A[r];
                ans1 += Max(dif1, dif2);
                r--;
                if (l > r) break;

                dif1 = Abs(lv - A[l]);
                dif2 = Abs(rv - A[l]);
                if (dif1 > dif2) lv = A[l];
                else rv = A[l];
                ans1 += Max(dif1, dif2);
                l++;
            }
            l = 0; r = N - 2;
            lv = A[N - 1]; rv = A[N - 1];
            long ans2 = 0;
            while (l <= r)
            {
                var dif1 = Abs(lv - A[l]);
                var dif2 = Abs(rv - A[l]);
                if (dif1 > dif2) lv = A[l];
                else rv = A[l];
                ans2 += Max(dif1, dif2);
                l++;
                if (l > r) break;
                dif1 = Abs(lv - A[r]);
                dif2 = Abs(rv - A[r]);
                if (dif1 > dif2) lv = A[r];
                else rv = A[r];
                ans2 += Max(dif1, dif2);
                r--;

            }
            ans = Max(ans1, ans2);
            
        }
        {
            if (N % 2 == 1)
            {


                long ans1 = 0;
                {
                    var small = new List<long>();
                    var large = new List<long>();
                    for (int i = 0; i < N; i++)
                    {
                        if (i < N / 2) small.Add(A[i]);
                        else large.Add(A[i]);
                    }
                    large.Reverse();
                    if (large.Count > 1)
                    {
                        int last = large.Count - 1;
                        var temp = large[1];
                        large[1] = large[0];
                        large[0] = temp;
                    }

                    var B = new long[N];
                    for (int i = 0; i < N; i++)
                    {
                        if (i % 2 == 0)
                            B[i] = large[i / 2];
                        else
                            B[i] = small[i / 2];
                    }
                    for (int i = 0; i < N - 1; i++)
                    {
                        ans1 += Abs(B[i] - B[i + 1]);
                    }
                }
                long ans2 = 0;
                {
                    var small = new List<long>();
                    var large = new List<long>();
                    for (int i = 0; i < N; i++)
                    {
                        if (i < N / 2 + N % 2) small.Add(A[i]);
                        else large.Add(A[i]);
                    }
                    large.Reverse();
                    if (small.Count > 1)
                    {
                        int last = small.Count - 1;
                        var temp = small[1];
                        small[1] = small[0];
                        small[0] = temp;
                    }

                    var B = new long[N];
                    for (int i = 0; i < N; i++)
                    {
                        if (i % 2 == 0)
                            B[i] = small[i / 2];
                        else
                            B[i] = large[i / 2];
                    }
                    for (int i = 0; i < N - 1; i++)
                    {
                        ans2 += Abs(B[i] - B[i + 1]);
                    }
                }
                ans = Max(ans, Max(ans1, ans2));
            }
            else
            {
                long ans1 = 0;
                {
                    var small = new List<long>();
                    var large = new List<long>();
                    for (int i = 0; i < N; i++)
                    {
                        if (i < N / 2) small.Add(A[i]);
                        else large.Add(A[i]);
                    }
                    large.Reverse();
                    if (large.Count > 1)
                    {
                        int last = large.Count - 1;
                        var temp = large[last];
                        large[last] = large[0];
                        large[0] = temp;
                    }

                    var B = new long[N];
                    for (int i = 0; i < N; i++)
                    {
                        if (i % 2 == 0)
                            B[i] = large[i / 2];
                        else
                            B[i] = small[i / 2];
                    }
                    for (int i = 0; i < N - 1; i++)
                    {
                        ans1 += Abs(B[i] - B[i + 1]);
                    }
                }
                long ans2 = 0;
                {
                    var small = new List<long>();
                    var large = new List<long>();
                    for (int i = 0; i < N; i++)
                    {
                        if (i < N / 2 + N % 2) small.Add(A[i]);
                        else large.Add(A[i]);
                    }
                    large.Reverse();
                    if (small.Count > 1)
                    {
                        int last = small.Count - 1;
                        var temp = small[last];
                        small[last] = small[0];
                        small[0] = temp;
                    }

                    var B = new long[N];
                    for (int i = 0; i < N; i++)
                    {
                        if (i % 2 == 0)
                            B[i] = small[i / 2];
                        else
                            B[i] = large[i / 2];
                    }
                    for (int i = 0; i < N - 1; i++)
                    {
                        ans2 += Abs(B[i] - B[i + 1]);
                    }
                }
                ans = Max(ans, Max(ans1, ans2));
            }
            
        }
        WriteLine(ans);
    }
}



class Scanner
{
    public static string RString() => Console.ReadLine();
    public static int RInt() => ReadTuple<int>();
    public static long RLong() => ReadTuple<long>();
    public static double RDouble() => ReadTuple<double>();

    public static string[] RStrings() => Console.ReadLine().Split();
    public static int[] RInts() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
    public static long[] RLongs() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
    public static double[] RDoubles() => Array.ConvertAll(Console.ReadLine().Split(), double.Parse);
    public static int[] RInts(Func<string, int> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static long[] RLongs(Func<string, long> func) => Console.ReadLine().Split().Select(func).ToArray();
    public static double[] RDoubles(Func<string, double> func) => Console.ReadLine().Split().Select(func).ToArray();
    //public static string[] RStrings(Func<string, string> func) => Console.ReadLine().Split().Select(func).ToArray();

    public static T1 ReadTuple<T1>()
    {
        var r = RString();
        var r1 = (T1)Convert.ChangeType(r, typeof(T1));
        return r1;
    }
    public static (T1, T2) ReadTuple<T1, T2>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        return (r1, r2);
    }
    public static (T1, T2, T3) ReadTuple<T1, T2, T3>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        return (r1, r2, r3);
    }
    public static (T1, T2, T3, T4) ReadTuple<T1, T2, T3, T4>()
    {
        var r = RStrings();
        var r1 = (T1)Convert.ChangeType(r[0], typeof(T1));
        var r2 = (T2)Convert.ChangeType(r[1], typeof(T2));
        var r3 = (T3)Convert.ChangeType(r[2], typeof(T3));
        var r4 = (T4)Convert.ChangeType(r[3], typeof(T4));
        return (r1, r2, r3, r4);
    }
}