#include <iostream> 
#include <iomanip> 
#include <cctype> 
#include <string> 
#include <cmath> 
#include <algorithm> 
#include <vector> 
#include <set> 
#include <map>  
#define _GLIBCXX_DEBUG 
#define rep(i, n) for (int i = 0; i < (int)(n); i++) 
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) 
#define ALL(v) v.begin(), v.end() 
#define ll long long
using namespace std; 

int main() { 
	ios::sync_with_stdio(false); 

    ll n; cin >> n;
    ll sum = 0;
    vector<ll> a(n); rep(i, n) cin >> a[i];
    
    sort(ALL(a), greater<int>());
    
    rep(i, n - 1){
    	sum += a[i] * (n - i - 1);
    	sum -= a[i + 1] * (i + 1);
    }
    
    cout << sum << "\n";
    
	return 0; 
}
// #define _GLIBCXX_DEBUG

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) std::begin(x), std::end(x)

using namespace std;
void dump_func()
{
    cerr << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&h, Tail &&... t)
{
    cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...);
}
#define dump(...)                                                                                                     \
    cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), \
        cerr << "*/\n\n";
typedef long long ll;

const double G = 0.577215;
const ll INF = 10000000;
ll solve(ll n, ll m, vector<ll> a)
{
    map<ll, int> count;
    ll tmp = 0;
    auto reset = [&]() {
        while (count.find(tmp) != count.end())
            tmp++;
    };
    auto decl = [&](ll t) {
        assert(count.find(t) != count.end());
        count[t] -= 1;
        if (count[t] == 0) {
            count.erase(t);
            if (t < tmp)
                tmp = t;
        }
    };
    auto incl = [&](ll t) {
        count[t] += 1;
        if (t == tmp)
            reset();
    };
    REP(i, m)
    {
        incl(a[i]);
    }
    ll res = tmp;
    REP3(i, m, n)
    {
        if (res == 0)
            break;
        incl(a[i]);
        decl(a[i - m]);
        res = min(tmp, res);
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];
    ll res = solve(n, m, a);
    cout << res << '\n';
    return 0;
}
