#include <bits/stdc++.h>
#define ll long long
#define N int(200)
using namespace std;
ll dp[N+10][N+10][3];
vector<ll>v;
ll n, pos;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n;
    for(int i=1; i<=n; i++)
    {
        ll x;
        cin>>x;
        v.push_back(x);
    }
    v.push_back(0);
    sort(v.begin(),v.end());
    ll pos = lower_bound(v.begin(), v.end(), 0) - v.begin();
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
        {
            dp[i][j][0]=1e9;
            dp[i][j][1]=1e9;
        }
    dp[pos][pos][0]=dp[pos][pos][1]=0;
    ll l=n+1;
    for(int len=1; len<=l; len++)
    {
        for(int i=0; i<(l-len+1); i++)
        {
            ll j=i+len-1;
            ll cl=l-len;
            if(dp[i][j][0]<1e9)
            {

                if(i>0)
                {
                    ll dif=abs(v[i]-v[i-1]);
                    ll them=dif*cl;
                    dp[i-1][j][0]=min( dp[i-1][j][0], dp[i][j][0]+them);
                }
                if(j<l)
                {
                    ll dif=abs(v[i]-v[j+1]);
                    ll them=dif*cl;
                    dp[i][j+1][1]=min( dp[i][j+1][1],dp[i][j][0]+them);
                }
            }
            if(dp[i][j][1]<1e9)
            {
                if(i>0)
                {
                    ll dif=abs(v[j]-v[i-1]);
                    ll them=dif*cl;
                    dp[i-1][j][0]=min( dp[i-1][j][0], dp[i][j][1]+them);
                }
                if(j<l)
                {
                    ll dif=abs(v[j]-v[j+1]);
                    ll them=dif*cl;
                    dp[i][j+1][1]=min( dp[i][j+1][1],dp[i][j][1]+them);
                }
            }
        }
    }
    cout<<min(dp[0][n][0],dp[0][n][1]);
    return 0;
}

