#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <list>
#include <sstream>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

#define deb(x) cerr << #x << " = " << x << endl;
#define print(Z,a,b) for (int __z = a; __z < b; __z ++) cout << Z[__z] << " ";
#define scan(Z,a,b) for (int __z = a; __z < b; __z ++) cin >> Z[__z];
#define bit(_z) (1ll << (_z))
#define rep(__z,__Z) for(int __z = 0; __z < __Z ; __z++ )
#define all(__z) __z.begin(),__z.end()

#define par pair<int, int>
#define p1 first
#define p2 second

#define eps = 1e-6

using namespace std;

int n, k;
int A[600100];
int DP[6000][6000];
int a;

int f(int alpha, int beta, int idx)
{
	while(alpha < 6000);
	while(beta < 6000);

	if (alpha == 0 and beta == 0)
		return 0;
	if (DP[alpha][beta] == -1)
	{
		DP[alpha][beta] = 1 << 30;
		if (alpha)
			DP[alpha][beta] = A[idx + a-1] - A[idx] + f(alpha-1, beta, idx+a);
		if (beta)
		{
			DP[alpha][beta] = min(DP[alpha][beta], A[idx + a] - A[idx] + f(alpha, beta-1, idx+a+1));
		}
	}
	return DP[alpha][beta];
}

int main()
{
	cin >> n >> k;
	memset(DP, -1, sizeof(DP));
	scan(A, 0, n);
	sort(A, A + n);

	int alpha, beta;
	a = n / k;
	for (alpha = 1; alpha < k; alpha ++)
	{
		if (((n - alpha * a)%(a+1)) == 0)
		{
			beta = (n - alpha * a) / (a+1);
			break ;
		}
	}
	//deb(a) deb(alpha); deb(beta);
	cout << f(alpha, beta, 0) << endl;

	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <list>
#include <sstream>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

#define print(Z,a,b) for (int __z = a; __z < b; __z ++) cout << Z[__z] << " ";
#define scan(Z,a,b) for (int __z = a; __z < b; __z ++) cin >> Z[__z];
#define bit(_z) (1ll << (_z))
#define rep(__z,__Z) for(int __z = 0; __z < __Z ; __z++ )
#define all(__z) __z.begin(),__z.end()

#define par pair<int, int>
#define p1 first
#define p2 second

#define eps = 1e-6

using namespace std;

int n, k;
long long A[500100], x, X=1;
long long C[500100][2];

int main()
{
	cin >> n >> k >> x;
	scan(A, 0, n);

	//sort(A, A+n);

	for (int i = 0; i < k; i ++)
	{
		X *= x;
	}

	//cerr << X << endl;

	for (int i = n-1; i > 0; i --)
	{
		C[i][1] = C[i+1][1] | A[i];
	}

	C[0][0] = A[0];
	for (int i = 1; i < n; i ++)
	{
		C[i][0] = C[i-1][0] | A[i];
		//cerr << C[i][0] << " " << C[i][1] << endl;
	}

	long long ans = 0;
	for (int i = 0; i < n; i ++)
	{
		ans = max(ans, (i>0?C[i-1][0]:0ll) | (X * A[i]) | C[i+1][1] );
	}

	cout << ans << endl;

	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <utility>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <list>
#include <sstream>
#include <time.h>
#include <stdlib.h>
#include <ctype.h>

#define deb(x) cerr << #x << " = " << x << endl;
#define print(Z,a,b) for (int __z = a; __z < b; __z ++) cout << Z[__z] << " ";
#define scan(Z,a,b) for (int __z = a; __z < b; __z ++) cin >> Z[__z];
#define bit(_z) (1ll << (_z))
#define rep(__z,__Z) for(int __z = 0; __z < __Z ; __z++ )
#define all(__z) __z.begin(),__z.end()

#define par pair<int, int>
#define p1 first
#define p2 second

#define eps = 1e-6

using namespace std;

int n, k;
int A[600100];
int* DP[301000];
int a;

int f(int alpha, int beta, int idx)
{

	if (alpha == 0 and beta == 0)
		return 0;
	if (DP[alpha][beta] == -1)
	{
		DP[alpha][beta] = 1 << 30;
		if (alpha)
			DP[alpha][beta] = A[idx + a-1] - A[idx] + f(alpha-1, beta, idx+a);
		if (beta)
		{
			DP[alpha][beta] = min(DP[alpha][beta], A[idx + a] - A[idx] + f(alpha, beta-1, idx+a+1));
		}
	}
	return DP[alpha][beta];
}

int main()
{
	cin >> n >> k;
	scan(A, 0, n);
	sort(A, A + n);

	int alpha, beta = 0;
	a = n / k;
	for (alpha = 1; alpha <= k; alpha ++)
	{
		if (((n - alpha * a)%(a+1)) == 0)
		{
			//deb((n - alpha * a) / (a+1))
			beta = (n - alpha * a) / (a+1);
			break ;
		}
	}
	//deb(a) deb(alpha); deb(beta);

	for (int i = 0; i <= alpha+1; i ++)
	{
		DP[i] = new int[beta+2];
		memset(DP[i], -1, sizeof(DP[i]));
	}

	cout << f(alpha, beta, 0) << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
bool cmp(long long int x,long long int y)
{
    if(cnt[x]==1) return true;
    if(cnt[y]==1) return false;
    return cnt[x]>cnt[y];
}
long long int a[100100];
vector<long long int> b;
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0;
    sort(b.begin(),b.end(),cmp);
    long long int cur=0;
    while(cnt[b[cur]]==1) {n--; cur++;}
    ans+=a[n];
    for(i=n;i>=1;i--)
    {
        if(cnt[b[cur]]==0){
            cur++; ans+=a[i];
        }
        if(cnt[b[cur]]==1){
            ans-=a[i];
        }
        cnt[b[cur]]--;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[100100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=n;i++)
    for(long long int j=0;j<=n;j++) f[i][j]=1LL<<50;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i][j+1]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[300100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n || n==1) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=n;i++)
    for(long long int j=0;j<=n;j++) f[i][j]=1LL<<50;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i][j+1]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[300100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n || n==1) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=k;i++)
    for(long long int j=0;j<=k;j++) f[i][j]=1LL<<50;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==(int)b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i][j+1]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[100100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=n;i++)
    for(long long int j=0;j<=n;j++) f[i][j]=1LL<<50;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i][j+1]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[300100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n || n==1) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=k;i++)
    for(long long int j=0;j<=k;j++) f[i][j]=1LL<<51;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(long long int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==(long long int)b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i+1][j]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
long long int cnt[50000];
long long int a[300100];
long long int f[5010][5010];
vector<long long int> b;
bool cmp(long long int x,long long int y)
{
    return cnt[x]<cnt[y];
}
int main()
{
    long long int n,i,k;
    //freopen("test.inp","r",stdin);
    scanf("%lld %lld",&n,&k);
    if(k>=n) {printf("0"); return 0;}
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    sort(a+1,a+1+n);
    for(i=0;i<=n;i++)
    for(long long int j=0;j<=n;j++) f[i][j]=1LL<<50;
    for(i=0;i<k;i++) b.push_back(i);
    for(i=n;i>=1;i--)
    {
        cnt[i%k]++;
    }
    long long int ans=0,c=0,sm;
    sort(b.begin(),b.end(),cmp);
    b.push_back(b[(int)b.size()-1]);
    for(i=0;i+1<b.size();i++)
    {
        if(cnt[b[i]]==cnt[b[i+1]]){
            c++; if(i==b.size()-2) {sm=cnt[b[i]]; break;}
        }
        else {c++; sm=cnt[b[i]]; break;}
    }
    f[0][0]=0;
    for(i=0;i<=c;i++)
    for(long long int j=0;j<=k-c;j++)
    {
        long long int cur=i*sm+j*(sm+1);
        //printf("%lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],c,k);
        if(j+1<=k-c)
        f[i][j+1]=min(f[i][j]+a[cur+sm+1]-a[cur+1],f[i][j+1]);
        //printf("%lld %lld %lld %lld %lld %lld %lld\n",i,j,cur,f[i][j],sm,c,k);
        if(i+1<=c){
        if(sm!=1)
        f[i+1][j]=min(f[i][j]+a[cur+sm]-a[cur+1],f[i][j+1]);
        else f[i+1][j]=min(f[i][j],f[i+1][j]);
        }
    }
    printf("%lld",f[c][k-c]);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair< ll , ll > ii ;
#define pb push_back
#define mp make_pair
#define se second
#define fi first
ll n,k;
ll a[300100];
ll p[300100];
ll dp[3010][3010];
ll solve(ll lar,ll smal)
{

    if(lar==(n%k)&&smal==k-(n%k))
        return 0;
    if(lar>(n%k)||smal>k-(n%k))return 99999999;
    if(dp[lar][smal]!=1000000000000)
        return dp[lar][smal];
    ll sol=99999999;
    ll pos=lar*((n/k)+1);
    pos+=(smal*(n/k));
    if(lar<n%k)
    {

        ll nextpos=pos+(n/k)+1;

             ll ww=0;
          ww=(a[nextpos-1]-a[pos]);
     //       cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=solve(lar+1,smal)+ww;
    }
    if(smal<k-(n%k))
    {
         ll nextpos=pos+n/k;

          ll ww=0;
         ww=(a[nextpos-1]-a[pos]);
       //        cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=min(sol,solve(lar,smal+1)+ww);

    }
    return dp[lar][smal]=sol;

}
int main()
{
    ios::sync_with_stdio(false);
    for(int i=0;i<=3000;i++)
    {
        for(int j=0;j<=3000;j++)
            dp[i][j]=1000000000000;
    }
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    sort(a,a+n);
    cout<<solve(0,0)<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair< ll , ll > ii ;
#define pb push_back
#define mp make_pair
#define se second
#define fi first
ll n,k;
ll a[300100];
ll p[300100];
ll dp[5010][5010];
ll solve(ll lar,ll smal)
{

    if(lar==(n%k)&&smal==k-(n%k))
        return 0;
    if(lar>(n%k)||smal>k-(n%k))return 9999999999999;
    if(dp[lar][smal]!=1000000000000)
        return dp[lar][smal];
    ll sol=999999999999;
    ll pos=lar*((n/k)+1);
    pos+=(smal*(n/k));
    if(lar<n%k)
    {

        ll nextpos=pos+(n/k)+1;

             ll ww=0;
          ww=(a[nextpos-1]-a[pos]);
     //       cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=solve(lar+1,smal)+ww;
    }
    if(smal<k-(n%k))
    {
         ll nextpos=pos+n/k;

          ll ww=0;
         ww=(a[nextpos-1]-a[pos]);
       //        cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=min(sol,solve(lar,smal+1)+ww);

    }
    return dp[lar][smal]=sol;

}
int main()
{
    ios::sync_with_stdio(false);
    for(int i=0;i<=5000;i++)
    {
        for(int j=0;j<=5000;j++)
            dp[i][j]=1000000000000;
    }
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    sort(a,a+n);
    cout<<solve(0,0)<<endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
typedef pair< ll , ll > ii ;
#define pb push_back
#define mp make_pair
#define se second
#define fi first
ll n,k;
ll a[300100];
ll p[300100];
ll dp[3010][3010];
ll solve(ll lar,ll smal)
{

    if(lar==(n%k)&&smal==k-(n%k))
        return 0;
    if(lar>(n%k)||smal>k-(n%k))return 9999999999999;
    if(dp[lar][smal]!=1000000000000)
        return dp[lar][smal];
    ll sol=999999999999;
    ll pos=lar*((n/k)+1);
    pos+=(smal*(n/k));
    if(lar<n%k)
    {

        ll nextpos=pos+(n/k)+1;

             ll ww=0;
          ww=(a[nextpos-1]-a[pos]);
     //       cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=solve(lar+1,smal)+ww;
    }
    if(smal<k-(n%k))
    {
         ll nextpos=pos+n/k;

          ll ww=0;
         ww=(a[nextpos-1]-a[pos]);
       //        cout<<lar<<" "<<smal<<"  "<<nextpos<<"  "<<ww<<endl;
        sol=min(sol,solve(lar,smal+1)+ww);

    }
    return dp[lar][smal]=sol;

}
int main()
{
    ios::sync_with_stdio(false);
    for(int i=0;i<=3000;i++)
    {
        for(int j=0;j<=3000;j++)
            dp[i][j]=1000000000000;
    }
    cin>>n>>k;
    for(int i=0;i<n;i++)
    {
      cin>>a[i];
    }
    sort(a,a+n);
    cout<<solve(0,0)<<endl;
    return 0;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

ll a[300005],b[300005];

int main()
{ios_base::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
rep(i,1,n)
cin>>a[i];
sort(a+1,a+n+1);
vector<pll> v;ll diff=0;
int g=n/k;
if(n%k!=0)
g++;
//ol(g);on;
for(int j=0;j+g-1<=n-1;j+=g)
{

  rep(i,j+1,j+g-1)
  {
    diff+=abs(a[i]-a[i+1]);
    //ol(diff);on;
  }
}
ll x=g*(n/g)+1;
if(x<n)
{
    rep(i,x,n-1)
    diff+=abs(a[i]-a[i+1]);
}
reverse(a+1,a+n+1);ll diff1=0;
/*rep(i,1,n)
{
    ol(a[i]);os;
}
on;*/
for(int j=0;j+g-1<=n-1;j+=g)
{

  rep(i,j+1,j+g-1)
  {
    diff1+=abs(a[i]-a[i+1]);
    //o2(a[i],a[i+1]);on;
  }
}
x=g*(n/g)+1;
if(x<n)
{
    rep(i,x,n-1)
    diff1+=abs(a[i]-a[i+1]);
}
ol(min(diff,diff1));
return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

ll a[300005],dp[5005][5005]={0};

int main()
{ios_base::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
rep(i,1,n)
cin>>a[i];
sort(a+1,a+n+1);
rep(i,0,5004)rep(j,0,5004)dp[i][j]=1e10;
ll sg=n/k,lg=n/k+1;
dp[0][0]=0;
ll l=n%k,s=k-n%k;
if(sg==1)s=0;
rep(i,0,l)
{
	rep(j,0,s)
	{
		if(i==0&&j==0)
		continue;
		int pos=i*(n/k+1)+j*(n/k);
		if(i>0)
		{
		  dp[i][j]=min(dp[i][j],dp[i-1][j]+a[pos]-a[pos-n/k]);
		  //o2(pos,pos-n/k);on;
	    }
		if(j>0)
		{
		  dp[i][j]=min(dp[i][j],dp[i][j-1]+a[pos]-a[pos-n/k+1]);
		  //o2(pos,pos-n/k+1);on;
	    }
	}
}
ol(dp[l][s]);
return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

ll a[300005],dp[5005][5005]={0};

int main()
{ios_base::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
rep(i,1,n)
cin>>a[i];
sort(a+1,a+n+1);
rep(i,0,5004)rep(j,0,5004)dp[i][j]=1e10;
ll sg=n/k,lg=n/k+1;
dp[0][0]=0;
ll l=n%k,s=k-n%k;
if(sg==1)s=0;
rep(i,0,l)
{
    rep(j,0,s)
    {
        if(i==0&&j==0)
        continue;
        ll pos=i*(n/k+1)+j*(n/k);
        if(i>0)
        {
          dp[i][j]=min(dp[i][j],dp[i-1][j]+a[pos]-a[pos-n/k]);
          //o2(pos,pos-n/k);on;
        }
        if(j>0)
        {
          dp[i][j]=min(dp[i][j],dp[i][j-1]+a[pos]-a[pos-n/k+1]);
          //o2(pos,pos-n/k+1);on;
        }
    }
}
ol(dp[l][s]);
return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

ll a[300005],dp[5005][5005]={0};

int main()
{ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,k;
cin>>n>>k;
rep(i,1,n)
cin>>a[i];
sort(a+1,a+n+1);
rep(i,0,5004)rep(j,0,5004)dp[i][j]=1e10;
ll sg=n/k,lg=n/k+1;
dp[0][0]=0;
ll l=n%k,s=k-n%k;
rep(i,0,l)
{
    rep(j,0,s)
    {
        if(i==0&&j==0)
        continue;
        ll pos=i*(n/k+1)+j*(n/k);
        if(i>0)
        {
          dp[i][j]=min(dp[i][j],dp[i-1][j]+a[pos]-a[pos-n/k]);
          //o2(pos,pos-n/k);on;
        }
        if(j>0)
        {
          dp[i][j]=min(dp[i][j],dp[i][j-1]+a[pos]-a[pos-n/k+1]);
          //o2(pos,pos-n/k+1);on;
        }
    }
}
ol(dp[l][s]);
return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef vector<vector<ll> > mat;

ll a[300005],dp[5005][5005]={0};

int main()
{ios_base::sync_with_stdio(false);
ll n,k;
cin>>n>>k;
rep(i,1,n)
cin>>a[i];
sort(a+1,a+n+1);
rep(i,0,5004)rep(j,0,5004)dp[i][j]=1e10;
ll sg=n/k,lg=n/k+1;
dp[0][0]=0;
ll l=n%k,s=k-n%k;
rep(i,0,l)
{
    rep(j,0,s)
    {
        if(i==0&&j==0)
        continue;
        ll pos=i*(n/k+1)+j*(n/k);
        if(i>0)
        {
          dp[i][j]=min(dp[i][j],dp[i-1][j]+a[pos]-a[pos-n/k]);
          //o2(pos,pos-n/k);on;
        }
        if(j>0)
        {
          dp[i][j]=min(dp[i][j],dp[i][j-1]+a[pos]-a[pos-n/k+1]);
          //o2(pos,pos-n/k+1);on;
        }
    }
}
ol(dp[l][s]);
return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int64;

const int64 inf = 1LL << 60;
const int maxn = 300000 + 10, maxk = 5000 + 10;

int a[maxn];
int64 s[maxn], dp[maxk][maxk];

int main() {
    int n, k; scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for(int i = 0; i < maxk; i++)
        for(int j = 0; j < maxk; j++)
            dp[i][j] = inf;
    dp[0][0] = 0;
    for(int i = 0; i <= n % k; i++) {
        for(int j = 0; j <= k - n % k; j++) if(i || j) {
            int now = i * (n / k + 1) + j * (n / k);
            if(i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[now] - a[(i - 1) * (n / k + 1) + j * (n / k) + 1]);
            if(j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[now] - a[i * (n / k + 1) + (j - 1) * (n / k) + 1]);
        }
    }
    printf("%I64d\n", dp[n % k][k - n % k]);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int64;

const int64 inf = 1LL << 60;
const int maxn = 100000 + 10, maxk = 5000 + 10;

int a[maxn];
int64 s[maxn], dp[maxk][maxk];

int main() {
    int n, k; scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for(int i = 0; i < maxk; i++)
        for(int j = 0; j < maxk; j++)
            dp[i][j] = inf;
    dp[0][0] = 0;
    for(int i = 0; i <= n % k; i++) {
        for(int j = 0; j <= k - n % k; j++) if(i || j) {
            int now = i * (n / k + 1) + j * (n / k);
            if(i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[now] - a[(i - 1) * (n / k + 1) + j * (n / k) + 1]);
            if(j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[now] - a[i * (n / k + 1) + (j - 1) * (n / k) + 1]);
        }
    }
    printf("%I64d\n", dp[n % k][k - n % k]);
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long int64;

const int64 inf = 1LL << 60;
const int maxn = 100000 + 10, maxk = 5000 + 10;

int a[maxn];
int64 s[maxn], dp[maxk][maxk];

int main() {
    int n, k; scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for(int i = 0; i < maxk; i++)
        for(int j = 0; j < maxk
                j++)
            dp[i][j] = inf;
    dp[0][0] = 0;
    for(int i = 0; i <= n % k; i++) {
        for(int j = 0; j <= k - n % k; j++) if(i || j) {
            int now = i * (n / k + 1) + j * (n / k);
            if(i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + a[now] - a[(i - 1) * (n / k + 1) + j * (n / k) + 1]);
            if(j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[now] - a[i * (n / k + 1) + (j - 1) * (n / k) + 1]);
        }
    }
    printf("%I64d\n", dp[n % k][k - n % k]);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<list>
#include<queue>
#include<utility>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<math.h>
#include<set>
#include<stack>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
using namespace std;
long long dp[400000],big=2147483647214748364;
vector<int>vc;
int m,n;
std::map<int, std::map<int, long long> > my;
std::map<int, std::map<int, bool> > state;
long long dp_func(int ind,int rem)
{
    if(ind==n+1){
            if(rem)return big;
            return 0;
    }
    if(ind>(n+1))return big;
    if(state[ind][rem]!=0)return my[ind][rem];
    long long ret=big;
    state[ind][rem]=1;
    if(rem!=0)ret=min(ret,dp_func(ind+m+1,rem-1)+dp[ind]-dp[ind+m]);
   // if(ind==1)cout<<ret<<endl;
    ret=min(ret,dp_func(ind+m,rem)+dp[ind]-dp[ind+m-1]);
    return my[ind][rem]=ret;
}

int main()
{
    int i,j,k,l,rem;
    cin>>n>>k;
    m=n/k;
    rem=n%k;
    vc.push_back(-2147483647);
    for(i=1;i<=n;i++){
        cin>>l;
        vc.push_back(l);
    }
    sort(vc.begin(),vc.end());
    for(i=n-1;i>=1;i--){
        dp[i]=dp[i+1]+abs(vc[i]-vc[i+1]);

    }
  //  for(i=1;i<=n;i++)cout<<dp[i]<<endl;
   // m--;
    cout<<dp_func(1,rem);
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<list>
#include<queue>
#include<utility>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<math.h>
#include<set>
#include<stack>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
using namespace std;
long long dp[400000],big=2147483647214748364;
vector<int>vc;
int m,n;
std::map<int, std::map<int, long long> > my;
std::map<int, std::map<int, bool> > state;
long long dp_func(int ind,int rem)
{
    if(ind==n+1){
            if(rem)return big;
            return 0;
    }
    if(ind>(n+1))return big;
    if(state[ind][rem]!=0)return my[ind][rem];
    long long ret=big;
    state[ind][rem]=1;
    if(rem!=0)ret=min(ret,dp_func(ind+m+1,rem-1)+dp[ind]-dp[ind+m]);
   // if(ind==1)cout<<ret<<endl;
    ret=min(ret,dp_func(ind+m,rem)+dp[ind]-dp[ind+m-1]);
    return my[ind][rem]=ret;
}

int main()
{
    int i,j,k,l,rem;
    cin>>n>>k;
    m=n/k;
    rem=n%k;
    vc.push_back(-2147483647);
    for(i=1;i<=n;i++){
      scanf("%d",&l);
        vc.push_back(l);
    }
    sort(vc.begin(),vc.end());
    for(i=n-1;i>=1;i--){
        dp[i]=dp[i+1]+abs(vc[i]-vc[i+1]);

    }
  //  for(i=1;i<=n;i++)cout<<dp[i]<<endl;
   // m--;
    cout<<dp_func(1,rem);
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include<map>
#include<list>
#include<queue>
#include<utility>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<math.h>
#include<set>
#include<stack>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<iterator>
using namespace std;
long long dp[5005][5005],b,s,big=2147483647214748,n,add;
vector<long long >vc;
long long dp_func(int v,int w)
{
    if(v>s || w>b)return big;
    if(v==s&&w==b)return 0;
    if(dp[v][w]!=-1)return dp[v][w];
    long long ret=big;
    int ind=(add*v)+(add+1)*w;
    if((ind+add-1)<n){
        ret=min(ret,dp_func(v+1,w)+vc[ind+add-1]-vc[ind]);

    }
    if((ind+add)<n){
        ret=min(ret,dp_func(v,w+1)+vc[ind+add]-vc[ind]);

    }
    return dp[v][w]=ret;

}
int main()
{
    int i,j,k,l,m;
    cin>>n>>k;
    add=n/k;
    s=k-(n%k);
    b=n%k;
    memset(dp,-1,sizeof dp);
    for(i=1;i<=n;i++){
        cin>>l;
        vc.push_back(l);
    }
    sort(vc.begin(),vc.end());
    cout<<dp_func(0,0);

}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int ngroups = k;
    int nbig = n % k;
    int small_size = n / k;

    map<int, long long> costs;
    costs[0] = 0;
    for (int i = 0; i < ngroups; i++) {
        map<int, long long> new_costs;
        for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
            int old_idx = it->first;
            long long old_cost = it->second;
            if (new_costs.count(old_idx + small_size) == 0) {new_costs[old_idx + small_size] = 1e12;}
            if (old_idx + small_size - 1 < n) {
                new_costs[old_idx + small_size] = min(new_costs[old_idx + small_size], old_cost + cumul_diffs[old_idx + small_size - 1] - cumul_diffs[old_idx]);
            }
            // FIX
            if (new_costs.count(old_idx + small_size + 1) == 0) {new_costs[old_idx + small_size + 1] = 1e12;}
            if (old_idx + small_size < n and new_costs.size()) {
                new_costs[old_idx + small_size + 1] = min(new_costs[old_idx + small_size + 1], old_cost + cumul_diffs[old_idx + small_size] - cumul_diffs[old_idx]);
            }
        }
        costs = new_costs;

        if (DEBUG) {
            cout << "\ncosts after doing i = " << i << "\t\n{";
            for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
                cout << "(" << it->first << ", " << it->second << "), ";
            }
            cout << "}";
        }
    }

    cout << costs[n];
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
long long dp_table[2][5001];
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int ngroups = k;
    int nbig = n % k;
    int small_size = n / k;

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j <= 5000; j++) {
            dp_table[i][j] = 1e12;
        }
    }

    dp_table[0][0] = 0;
    for (int i = 1; i <= ngroups; i++) {
        // About to write into i%2, erase the other one
        for (int j = 0; j <= 5000; j++) {
            dp_table[i%2][j] = 1e12;
        }
        for (int j = 0; j <= nbig; j++) {
            int new_idx = i * small_size + j;
            dp_table[i%2][j] = min(dp_table[i%2][j], dp_table[1 - i%2][j] + cumul_diffs[new_idx - 1] - cumul_diffs[new_idx - small_size]);
            if (j > 0) {
                dp_table[i%2][j] = min(dp_table[i%2][j], dp_table[1 - i%2][j-1] + cumul_diffs[new_idx - 1] - cumul_diffs[new_idx - small_size - 1]);
            }
        }

        if (DEBUG) {
            cout << "\ndp_table:";
            for (int j = 0; j < 2; j++) {
                cout << "\n";
                for (int i = 0; i < n; i++) {
                    cout << dp_table[j][i] << " ";
                }
            }
        }
    }

    cout << dp_table[ngroups%2][nbig];
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
int main() {
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int ngroups = k;
    int nbig = n % k;
    int small_size = n / k;

    map<int, long long> costs;
    costs[0] = 0;
    for (int i = 0; i < ngroups; i++) {
        map<int, long long> new_costs;
        for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
            int old_idx = it->first;
            long long old_cost = it->second;
            if (new_costs.count(old_idx + small_size) == 0) {new_costs[old_idx + small_size] = 1e12;}
            if (old_idx + small_size - 1 < n) {
                new_costs[old_idx + small_size] = min(new_costs[old_idx + small_size], old_cost + cumul_diffs[old_idx + small_size - 1] - cumul_diffs[old_idx]);
            }
            if (new_costs.count(old_idx + small_size + 1) == 0 and new_costs.size() < 5010) {
                new_costs[old_idx + small_size + 1] = 1e12;
                if (old_idx + small_size < n) {
                    new_costs[old_idx + small_size + 1] = min(new_costs[old_idx + small_size + 1], old_cost + cumul_diffs[old_idx + small_size] - cumul_diffs[old_idx]);
                }
            }
        }
        costs = new_costs;

        if (DEBUG) {
            cout << "\ncosts after doing i = " << i << "\t\n{";
            for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
                cout << "(" << it->first << ", " << it->second << "), ";
            }
            cout << "}";
        }
    }

    cout << costs[n];
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
long long dp[10001][5001]; // Cost of getting up to (but not including) n with [nbig] groups left
int main() {
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int nbig = n % k;
    int small_size = n / k;

    // initialize
    for (int i = 0; i < 10012; i++) {
        for (int j = 0; j < 5001; j++) {
            dp[i%10000][j] = 2e9 + 1;
        }
    }

    dp[0][nbig] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5001; j++) {
            // Can we use a small group?
            if (i + small_size <= n) {
                int this_cost = cumul_diffs[i + small_size - 1] - cumul_diffs[i];
                dp[(i + small_size) % 10000][j] = min(dp[(i + small_size) % 10000][j], dp[i % 10000][j] + this_cost);
            }
            if (j > 0 and i + small_size + 1 <= n) {
                int this_cost = cumul_diffs[i + small_size] - cumul_diffs[i];
                dp[(i + small_size + 1) % 10000][j - 1] = min(dp[(i + small_size + 1) % 10000][j - 1], dp[i%10000][j] + this_cost);
            }
        }
    }

    cout << dp[n%10000][0];

}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
int dp[10001][5001]; // Cost of getting up to (but not including) n with [nbig] groups left
int main() {
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int nbig = n % k;
    int ngroups = min(n, k);
    int small_size = n / k;

    map<int, long long> costs;
    costs[0] = 0;
    for (int i = 0; i < ngroups; i++) {
        map<int, long long> new_costs;
        for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
            int old_idx = it->first;
            long long old_cost = it->second;
            if (new_costs.count(old_idx + small_size) == 0) {new_costs[old_idx + small_size] = 1e12;}
            if (old_idx + small_size - 1 < n) {
                new_costs[old_idx + small_size] = min(new_costs[old_idx + small_size], old_cost + cumul_diffs[old_idx + small_size - 1] - cumul_diffs[old_idx]);
            }
            if (new_costs.count(old_idx + small_size + 1) == 0) {new_costs[old_idx + small_size + 1] = 1e12;}
            if (old_idx + small_size < n) {
                new_costs[old_idx + small_size + 1] = min(new_costs[old_idx + small_size + 1], old_cost + cumul_diffs[old_idx + small_size] - cumul_diffs[old_idx]);
            }
        }
        costs = new_costs;

        if (DEBUG) {
            cout << "\ncosts after doing i = " << i << "\t\n{";
            for (map<int, long long>::iterator it = costs.begin(); it != costs.end(); ++it) {
                cout << "(" << it->first << ", " << it->second << "), ";
            }
            cout << "}";
        }
    }

    cout << costs[n];
/*
    // initialize
    for (int i = 0; i < 10012; i++) {
        for (int j = 0; j < 5001; j++) {
            dp[i%10000][j] = 2e9 + 1;
        }
    }

    dp[0][nbig] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5001; j++) {
            // Can we use a small group?
            if (i + small_size <= n) {
                int this_cost = cumul_diffs[i + small_size - 1] - cumul_diffs[i];
                dp[(i + small_size) % 10000][j] = min(dp[(i + small_size) % 10000][j], dp[i % 10000][j] + this_cost);
            }
            if (j > 0 and i + small_size + 1 <= n) {
                int this_cost = cumul_diffs[i + small_size] - cumul_diffs[i];
                dp[(i + small_size + 1) % 10000][j - 1] = min(dp[(i + small_size + 1) % 10000][j - 1], dp[i%10000][j] + this_cost);
            }
        }
    }

    cout << dp[n%10000][0];
*/
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
int dp[10001][5001]; // Cost of getting up to (but not including) n with [nbig] groups left
int main() {
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int nbig = n % k;
    int small_size = n / k;

    // initialize
    for (int i = 0; i < 10012; i++) {
        for (int j = 0; j < 5001; j++) {
            dp[i%10000][j] = 2e9 + 1;
        }
    }

    dp[0][nbig] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5001; j++) {
            // Can we use a small group?
            if (i + small_size <= n) {
                int this_cost = cumul_diffs[i + small_size - 1] - cumul_diffs[i];
                dp[(i + small_size) % 10000][j] = min(dp[(i + small_size) % 10000][j], dp[i % 10000][j] + this_cost);
            }
            if (j > 0 and i + small_size + 1 <= n) {
                int this_cost = cumul_diffs[i + small_size] - cumul_diffs[i];
                dp[(i + small_size + 1) % 10000][j - 1] = min(dp[(i + small_size + 1) % 10000][j - 1], dp[i%10000][j] + this_cost);
            }
        }
    }

    cout << dp[n%10000][0];

}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <map>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
long long dp_table[2][5001];
int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> k;
    a.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int ngroups = k;
    int nbig = n % k;
    int small_size = n / k;

    dp_table[0][0] = 0;
    for (int i = 1; i <= ngroups; i++) {
        // About to write into i%2, erase the other one
        for (int j = 0; j <= 5000; j++) {
            dp_table[i%2][j] = 1e12;
        }
        for (int j = 0; j <= nbig; j++) {
            int new_idx = i * small_size + j;
            dp_table[i%2][j] = min(dp_table[i%2][j], dp_table[1 - i%2][j] + cumul_diffs[new_idx - 1] - cumul_diffs[new_idx - small_size]);
            if (j > 0) {
                dp_table[i%2][j] = min(dp_table[i%2][j], dp_table[1 - i%2][j-1] + cumul_diffs[new_idx] - cumul_diffs[new_idx - small_size]);
            }
        }

        if (DEBUG) {
            ;
        }
    }

    cout << dp_table[ngroups%2][nbig];
}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const bool DEBUG = false;
const long long MAXN = 3 * 1e5 + 1;

int n, k;
vector<int> a;
vector<int> cumul_diffs;
int dp[10001][5001]; // Cost of getting up to (but not including) n with [nbig] groups left
int main() {
    cin >> n >> k;
    a.resize(n);long long this_cost = cumul_diffs[i + small_size - 1] - cumul_diffs[i];
                dp[i + small_size][j] = min(dp[i + small_size][j], dp[i][j] + this_cost);

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    sort(a.begin(), a.end());

    cumul_diffs.resize(n);
    cumul_diffs[0] = 0;
    for (int i = 1; i < n; i++) {
        cumul_diffs[i] = cumul_diffs[i-1] + abs(a[i] - a[i-1]);
    }

    if (DEBUG) {
        cout << "\ncumul_diffs: ";
        for (int i = 0; i < cumul_diffs.size(); i++) {cout << cumul_diffs[i] << " ";}
    }

    int nbig = n % k;
    int small_size = n / k;

    // initialize
    for (int i = 0; i < 10012; i++) {
        for (int j = 0; j < 5001; j++) {
            dp[i%10000][j] = 2e9 + 1;
        }
    }

    dp[0][nbig] = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < 5001; j++) {
            // Can we use a small group?
            if (i + small_size <= n) {
                int this_cost = cumul_diffs[i + small_size - 1] - cumul_diffs[i];
                dp[(i + small_size) % 10000][j] = min(dp[(i + small_size) % 10000][j], dp[i % 10000][j] + this_cost);
            }
            if (j > 0 and i + small_size + 1 <= n) {
                int this_cost = cumul_diffs[i + small_size] - cumul_diffs[i];
                dp[(i + small_size + 1) % 10000][j - 1] = min(dp[(i + small_size + 1) % 10000][j - 1], dp[i%10000][j] + this_cost);
            }
        }
    }

    cout << dp[n%10000][0];

}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
LL a[mmax];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*mmax;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*mmax)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
int a[mmax];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*inf;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*inf)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
int a[3100000];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*inf;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*inf)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
//                    if((i+j+1)*len+i+1>n)
//                    {
//                        cout<<"0"<<endl;
//                        return 0;
//                    }
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
//                    if((i+j+1)*len+i >n )
//                    {
//                        cout<<"0"<<endl;
//                        return 0;
//                    }
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
LL a[mmax];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*inf;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*inf)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
                    if((i+j+1)*len+i+1>n)
                    {
                        cout<<"0"<<endl;
                        return 0;
                    }
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
                    if((i+j+1)*len+i >n )
                    {
                        cout<<"0"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
int a[mmax];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*mmax;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*mmax)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<stdlib.h>
#include<set>
#include<ctime>
#include<cmath>
#include <cstdlib>
using namespace std;
typedef __int64 LL;
const int  mmax  = 5010;
const int inf = 0x3fffffff;
LL dp[mmax][mmax];
LL a[mmax];
int n,k;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    int len=n/k;
    int cnt1=n%k;
    int cnt2=k-cnt1;
    for(int i=0;i<=cnt1;i++)
        for(int j=0;j<=cnt2;j++)
        {
            dp[i][j]=1LL*inf*inf;
        }
    sort(a+1,a+n+1);
    dp[0][0]=0;
    for(int i=0;i<=cnt1;i++)
    {
        for(int j=0;j<=cnt2;j++)
        {
            if(dp[i][j]<1LL*inf*inf)
            {
                if(i+1<=cnt1)
                {
                    dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]);
                    if(a[(i+j+1)*len+i+1]-a[(i+j)*len+i+1]<0)
                    {
                        cout<<"0"<<endl;
                        return 0;
                    }
                }
                if(j+1<=cnt2)
                {
                    dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[(i+j+1)*len+i]-a[(i+j)*len+i+1]);
                    if(a[(i+j+1)*len+i]-a[(i+j)*len+i+1]<0)
                    {
                        cout<<"0"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    cout<<dp[cnt1][cnt2]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1e6+5;

int t1, t2, a[N], t[N], x, y, n, m, k, lll[N];
vector< int > dp[N];

ll f(int x, int y, int w) {
    if(x < 0 || y < 0) return linf;
    if(w > n) return 0;
    int &r = dp[x][y];
    if(r != -1) return r;
    return r = min(f(x,y-1,w+t2)+a[w+t2-1]-a[w], f(x-1,y,w+t1)+a[w+t1-1]-a[w]);
}

int main() {
    scanf("%d %d",&n,&k);
    FOR(i, 1, n)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    t1 = 0, t2 = 0;
    FOR(i, 1, n) {
        t[i%k]++;
    }
    FOR(i, 0, k-1)
        lll[t[i]]++;
    FOR(i, 0, k-1)
        if(t[i] && t1 && t[i] != t1)
            t2 = t[i];
        else if(t[i])
            t1 = t[i];

    if(!t2) {
        int last = 1;
        ll ans = 0;
        FOR(i, 1, n) {
            if(i % t1 == 0) {
                ans += a[i] - a[last];
                last = i+1;
            }
        }
        if(last != n+1) ans += a[n] - a[last];
        cout << ans << endl;
        return 0;
    }
    FOR(i, 0, lll[t1] + 5)
        dp[i].resize(lll[t2]+5,-1);
    cout << f(lll[t1],lll[t2],1) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1e6+5;

int t1, t2, a[N], t[N], x, y, n, m, k;
vector< int > dp[N];

ll f(int x, int y, int w) {
    if(x < 0 || y < 0) return linf;
    if(w > n) return 0;
    int &r = dp[x][y];
    if(r != -1) return r;
    return r = min(f(x,y-1,w+t2)+a[w+t2-1]-a[w], f(x-1,y,w+t1)+a[w+t1-1]-a[w]);
}

int main() {
    scanf("%d %d",&n,&k);
    FOR(i, 1, n)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    t1 = 0, t2 = 0;
    FOR(i, 0, k-1) {
        if(t[i]){
            if(t1) t2 = i;
            else t1 = i;
        }
    }
    if(!t2) {
        int last = 1;
        ll ans = 0;
        FOR(i, 1, n) {
            if(i % k == 0) {
                ans += a[i] - a[last];
                last = i+1;
            }
        }
        if(last != n+1) ans += a[n] - a[last];
        cout << ans << endl;
        return 0;
    }
    FOR(i, 0, t[t1] + 5)
        dp[i].resize(t[t2]+5,-1);
    cout << f(t[t1],t[t2],1) << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = (int) 1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 1e6+5;

int t1, t2, a[N], t[N], x, y, n, m, k, lll[N];
vector< int > dp[N];

ll f(int x, int y, int w) {
    if(x < 0 || y < 0) return linf;
    if(w > n) return 0;
    int &r = dp[x][y];
    if(r != -1) return r;
    return r = min(f(x,y-1,w+t2)+a[w+t2-1]-a[w], f(x-1,y,w+t1)+a[w+t1-1]-a[w]);
}

int main() {
    scanf("%d %d",&n,&k);
    FOR(i, 1, n)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    t1 = 0, t2 = 0;
    FOR(i, 1, n) {
        t[i%k]++;
    }
    FOR(i, 0, n) {
       lll[t[i]]++;
    }
    FOR(i, 0, k-1)
        if(t[i] && t[i] == t1)
            t2 = i;
        else if(t[i])
            t1 = i;

    if(!t2) {
        int last = 1;
        ll ans = 0;
        FOR(i, 1, n) {
            if(i % t1 == 0) {
                ans += a[i] - a[last];
                last = i+1;
            }
        }
        if(last != n+1) ans += a[n] - a[last];
        cout << ans << endl;
        return 0;
    }
    FOR(i, 0, lll[t1] + 5)
        dp[i].resize(lll[t2]+5,-1);
    cout << f(lll[t1],lll[t2],1) << endl;
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define F first
#define S second
#define mod 1000000007
#define inf 0x7fffffff
#define N 300010
using namespace std;
typedef long long ll;

template<class T>
inline bool In(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template<class T>
inline void Out(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
/*********************************************************************/

int n,k,ans=0;
int a[N];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    In(n);In(k);
    for(int i=0;i<n;i++) In(a[i]);
    sort(a,a+n);
    int p=0,y=n%k,x=n/k;
    int a1=0,a2=0;
    if(x)
    for(int i=0;i<k;i++){
        if(i<y){a1+=a[p+x]-a[p];p=p+x+1;}
        else if(x>1){a1+=a[p+x-1]-a[p];p=p+x;}
    }
    reverse(a,a+n);
    p=0;
    if(x)
    for(int i=0;i<k;i++){
        if(i<y){a2+=a[p]-a[p+x];p=p+x+1;}
        else if(x>1){a2+=a[p]-a[p+x-1];p=p+x;}
    }
    ans=min(a1,a2);
    Out(ans);
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define F first
#define S second
#define mod 1000000007
#define inf 0x7fffffff
#define N 300010
using namespace std;
typedef long long ll;

template<class T>
inline bool In(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template<class T>
inline void Out(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
/*********************************************************************/

int n,k,ans=0;
int a[N];
int dp[2][5010];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    In(n);In(k);
    for(int i=0;i<n;i++) In(a[i]);
    sort(a,a+n);
    int y=n%k,x=n/k;
    for(int i=0;i<=k-y;i++){
            memset(dp[i&1],127,sizeof(dp[i&1]));
            if(!i) dp[0][0]=0;
        for(int j=0;j<=y;j++){
            int p=i*x+j*(x+1);
            if(i) dp[i&1][j]=min(dp[i&1][j],dp[(i-1)&1][j]+a[p-1]-a[p-x]);
            if(j) dp[i&1][j]=min(dp[i&1][j],dp[i&1][j-1]+a[p-1]-a[p-x-1]);
        }
    }
    Out(dp[(k-y)&1][y]);
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define F first
#define S second
#define mod 1000000007
#define inf 0x7fffffff
#define N 300010
using namespace std;
typedef long long ll;

template<class T>
inline bool In(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template<class T>
inline void Out(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
/*********************************************************************/

int n,k,ans=0;
int a[N];
int dp[5010][2];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    In(n);In(k);
    for(int i=0;i<n;i++) In(a[i]);
    sort(a,a+n);
    int y=n%k,x=n/k;
    memset(dp,127,sizeof(dp));
    dp[0][0]=0;
    for(int i=0;i<=k-y;i++){
        for(int j=0;j<=y;j++){
            int p=i*x+j*(x+1);
            if(i) dp[i][j&1]=min(dp[i][j&1],dp[i-1][j&1]+a[p-1]-a[p-x]);
            if(j) dp[i][j&1]=min(dp[i][j&1],dp[i][(j-1)&1]+a[p-1]-a[p-x-1]);
        }
    }
    Out(dp[k-y][y&1]);
    return 0;
}


#pragma comment(linker, "/STACK:102400000,102400000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define F first
#define S second
#define mod 1000000007
#define inf 0x7fffffff
#define N 300010
using namespace std;
typedef long long ll;

template<class T>
inline bool In(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template<class T>
inline void Out(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
/*********************************************************************/

int n,k,ans=0;
int a[N];
int dp[5010][5010];

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    In(n);In(k);
    for(int i=0;i<n;i++) In(a[i]);
    sort(a,a+n);
    int y=n%k,x=n/k;
    memset(dp,127,sizeof(dp));
    dp[0][0]=0;
    for(int i=0;i<=k-y;i++){
        for(int j=0;j<=y;j++){
            int p=i*x+j*(x+1);
            if(i) dp[i][j]=min(dp[i][j],dp[i-1][j]+a[p-1]-a[p-x]);
            if(j) dp[i][j]=min(dp[i][j],dp[i][j-1]+a[p-1]-a[p-x-1]);
        }
    }
    Out(dp[k-y][y]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,a[400000],k,m,l;
int ans,s[400000],*z;
map<pair<int,int>,int> t;
queue<pair<int,int> > q;
int main(){
    //freopen("vao.inp", "r", stdin);
    scanf("%d%d",&n,&k);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    if (n%k==0){
        l=n/k;
        for (int i=1; i<=n; i++)
        if ((i%l)!=1) {
            ans+=a[i]-a[i-1];
        }
        cout<<ans;
        return 0;
    }
    l=(n/k);
    m=n%k;
    for (int i=2; i<=n; i++) s[i]=s[i-1]+a[i]-a[i-1];
    t[make_pair(0,0)]=0;
    q.push(make_pair(0,0));
    int pos,cnt,tmp,cur;
    while (!q.empty()){
        pos=q.front().first;
        cnt=q.front().second;
        //cout<<pos<<" "<<cnt<<endl;
        q.pop();
        cur=t[make_pair(pos,cnt)];
        tmp=(pos-cnt*(l+1))/l;
        if (cnt<m){
            if (!t.count(make_pair(pos+l+1,cnt+1))) {
                t[make_pair(pos+l+1,cnt+1)]=2e9+10;
                q.push(make_pair(pos+l+1,cnt+1));
            }
            z=&t[make_pair(pos+l+1,cnt+1)];
            *z=min(*z,cur+s[pos+l+1]-s[pos+1]);
        }
        if (tmp<k-m){
            if (!t.count(make_pair(pos+l,cnt))) {
                t[make_pair(pos+l,cnt)]=2e9+10;
                q.push(make_pair(pos+l,cnt));
            }
            z=&t[make_pair(pos+l,cnt)];
            *z=min(*z,cur+s[pos+l]-s[pos+1]);
        }
    }
    cout<<t[make_pair(n,m)];
}


#include <bits/stdc++.h>
using namespace std;
int n,a[400000],k,m,l;
int ans,s[400000];
map<pair<int,int>,int> t;
queue<pair<int,int> > q;
int main(){
    //freopen("vao.inp", "r", stdin);
    scanf("%d%d",&n,&k);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    if (n%k==0){
        l=n/k;
        for (int i=1; i<=n; i++)
        if ((i%l)!=1) {
            ans+=a[i]-a[i-1];
        }
        cout<<ans;
        return 0;
    }
    l=(n/k);
    m=n%k;
    for (int i=2; i<=n; i++) s[i]=s[i-1]+a[i]-a[i-1];
    t[make_pair(0,0)]=0;
    q.push(make_pair(0,0));
    int pos,cnt,tmp,cur;
    while (!q.empty()){
        pos=q.front().first;
        cnt=q.front().second;
        q.pop();
        cur=t[make_pair(pos,cnt)];
        tmp=(pos-cnt*(l+1))/l;
        if (cnt<m){
            if (!t.count(make_pair(pos+l+1,cnt+1))) {
                t[make_pair(pos+l+1,cnt+1)]=2e9+10;
                q.push(make_pair(pos+l+1,cnt+1));
            }
            t[make_pair(pos+l+1,cnt+1)]=min(t[make_pair(pos+l+1,cnt+1)],cur+s[pos+l+1]-s[pos+1]);
        }
        if (tmp<k-m){
            if (!t.count(make_pair(pos+l,cnt))) {
                t[make_pair(pos+l,cnt)]=2e9+10;
                q.push(make_pair(pos+l,cnt));
            }
            t[make_pair(pos+l,cnt)]=min(t[make_pair(pos+l,cnt)],cur+s[pos+l]-s[pos+1]);
        }
    }
    cout<<t[make_pair(n,m)];
}


#include <bits/stdc++.h>
using namespace std;
int n,a[400000],k;
long long ans;
int main(){
    scanf("%d%d",&n,&k);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int p=1;
    for (int i=1; i<=k; i++){
        p++;
        for (int j=i+k; j<=n; j+=k) ans+=a[p]-a[p-1],p++;
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
int n,a[400000],k;
long long ans;
int main(){
    scanf("%d%d",&n,&k);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    int p=1;
    for (int i=1; i<=k; i++){
        p++;
        for (int j=i; j<=n; j+=k) ans+=a[p]-a[p-1],p++;
    }
    cout<<ans;
}


#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <set>
#include <utility>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;

int n, k, size, number, remainder;
int A[300000];
int diff[300000]; //diff[1] = A[1] - A[0];
long long X[5000][5000];

void makeDiff(){
    for (int i = 1; i < n; i++) {
        diff[i] = A[i] - A[i-1];
    }
}

void solve(bool rem){
    memset(X,-1,sizeof(X));
    X[0][0] = 0;
    X[0][1] = 0;
    for (int i = 1; i < k; i++) {
        for (int j = 0; j <= remainder; j++) {
            if (X[i-1][j] != -1) {
                X[i][j] = X[i-1][j] + diff[size * i + j];
            }
            if (rem && j - 1 >= 0 && X[i-1][j-1] != -1) {
                X[i][j] = max(X[i][j], X[i-1][j-1] + diff[size * i + j-1]);
            }
        }
    }
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    sort(A, A+n);
    makeDiff();
    size = n / k;
    number = k;
    remainder = n % k;

    solve(remainder == 1);

    cout << A[n-1] - A[0] - X[k-1][remainder];
    return 0;
}


#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <set>
#include <utility>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;

int n, k, size, number, remainder;
int A[300000];
int diff[300000]; //diff[1] = A[1] - A[0];
long long X[5000][5000];

void makeDiff(){
    for (int i = 1; i < n; i++) {
        diff[i] = A[i] - A[i-1];
    }
}

void solve(bool rem){
    memset(X,-1,sizeof(X));
    X[0][0] = 0;
    X[0][1] = 0;
    for (int i = 1; i < k; i++) {
        for (int j = 0; j <= remainder; j++) {
            if (X[i-1][j] != -1) {
                X[i][j] = X[i-1][j] + diff[size * i + j];
            }
            if (rem && j - 1 >= 0 && X[i-1][j-1] != -1) {
                X[i][j] = max(X[i][j], X[i-1][j-1] + diff[size * i + j-1]);
            }
        }
    }
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> A[i];
    }
    sort(A, A+n);
    makeDiff();
    size = n / k;
    number = k;
    remainder = n % k;

    solve(remainder != 0);

    cout << A[n-1] - A[0] - X[k-1][remainder];
    return 0;
}


#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#define maxn 100005
#define ll long long
using namespace std;
ll n,k,a[maxn];
ll b[maxn],totb,mul[maxn];
ll pri[maxn],tot;
ll num[maxn];
ll ans;
void init(){
    bool v[maxn];
    ll maxp=pow(10,(double)10/k)+1;
    for(int i=2;i<=maxp;i++){
        if(v[i]) continue;
        for(int j=i*2;j<=maxp;j+=i) v[j]=1;
        pri[++tot]=i;
    }
    ll cnt,tmp,tmpm;
    //cout<<maxp<<endl;
    for(int i=1;i<=n;i++){
        tmp=tmpm=1;
        for(int j=1;j<=tot;j++){
            cnt=0;
            while(!(a[i]%pri[j])) a[i]/=pri[j],cnt=(cnt+1)%k;
            tmp*=(ll)pow(pri[j],cnt);
            if(cnt) tmpm*=(ll)pow(pri[j],k-cnt);
            if(tmpm>maxn) break;
            if(a[i]==1){
                b[++totb]=tmp,num[tmp]++,mul[totb]=tmpm;
                break;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    init();

    /*for(int i=1;i<=totb;i++) cout<<b[i]<<' ';
    cout<<endl;
    for(int i=1;i<=totb;i++) cout<<mul[i]<<' ';
    cout<<endl;
    cout<<num[1]<<','<<num[3]<<','<<num[9]<<','<<num[27]<<endl;*/

    for(int i=1;i<=totb;i++) ans+=num[mul[i]];
    ans=(ans-num[1])/2;
    cout<<ans;
    return 0;
}


#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
#define maxn 200005
#define ll long long
using namespace std;
ll n,k,a[maxn];
ll pri[maxn],totp;
ll tot,mul[maxn],num[maxn];
ll ans;
inline ll npow(ll a,ll x){
    ll ret=1,tmp=a;
    while(x){
        if(x&1) ret*=tmp;
        tmp*=tmp,x>>=1;
        if(ret>maxn) return ret;
    }
    return ret;
}
void init(){
    bool v[maxn];
    ll maxp=pow(10,(double)10/k)+1;
    for(int i=2;i<maxp;i++){
        if(v[i]) continue;
        for(int j=i*2;j<maxp;j+=i) v[j]=1;
        pri[++totp]=i;
    }
}
void solve(){
    ll cnt,cur,rst;
    for(int i=1;i<=n;i++){
        cur=rst=1;
        for(int j=1;j<=totp;j++){
            cnt=0;
            while(!(a[i]%pri[j])) a[i]/=pri[j],cnt=(cnt+1)%k;
            cur*=npow(pri[j],cnt);
            rst*=npow(pri[j],(k-cnt)%k);
            if(rst>maxn||a[i]==1) break;
        }
        if(a[i]==1){
            num[cur]++,mul[++tot]=rst;
            ans-=cur==rst;
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    init();
    solve();
    for(int i=1;i<=tot;i++) ans+=num[mul[i]];
    ans>>=1;
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, b) for (int i = a, end = b; i <= end; i++)
#define repd(i, a, b) for (int i = a, end = b; i >= end; i--)
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define INF (1<<30)

const int maxn = 100000 + 5;

int N, K;
int a[maxn], Hash[maxn];
ll b[maxn];

int p[maxn], check[maxn];
void init() {
	rep(i, 2, 100000) {
		if (!check[i]) p[++p[0]] = i;
		for (register int j = 1; j <= p[0] && i * p[j] <= 100000; j++) {
			check[i*p[j]] = 1;
			if (!(i % p[j])) break;
		}
	}
}

int main() {
	scanf("%d%d", &N, &K);
	init();
	rep(i, 1, N) scanf("%d", &a[i]), b[i] = 1;
	rep(i, 1, p[0]) {
		ll wty = 1;
		int ok = 1;
		rep(j, 1, K) {
			wty *= p[i];
			if (wty >= 100000) { ok = 0; break; }
		}
		if (!ok) break;
		rep(j, 1, N)
			while (!(a[j] % wty)) a[j] /= wty;
	}
	rep(i, 1, N) {
		int x = a[i], pt = 1;
		if (x == 1) continue;
		while (check[x]) {
			if (!(x % p[pt])) {
				x /= p[pt];
				if (b[i] % p[pt]) b[i] *= p[pt];
			}
			else pt++;
		}
		if (b[i] % x) b[i] *= x;
	}
	rep(j, 1, N) Hash[a[j]]++;
	ll ans = 0;
	rep(j, 1, N) {
		ll wty = 1, ok = 1;
		rep(i, 1, K) {
			wty = wty * b[j];
			if (wty >= 1e10) { ok = 0; break; }
		}
		if (!ok) break;
		ll val = wty / a[j];
		if (val >= 100000) continue;
		ans += Hash[val];
		if (val == a[j]) ans--;
	}
	printf("%lld", ans/2);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, b) for (int i = a, end = b; i <= end; i++)
#define repd(i, a, b) for (int i = a, end = b; i >= end; i--)
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define INF (1<<30)

const int maxn = 100000 + 5;

int N, K;
int a[maxn], b[maxn], hash[maxn];

int p[maxn], check[maxn];
void init() {
	rep(i, 2, 100000) {
		if (!check[i]) p[++p[0]] = i;
		for (register int j = 1; j <= p[0] && i * p[j] <= 100000; j++) {
			check[i*p[j]] = 1;
			if (!(i % p[j])) break;
		}
	}
}

int main() {
	scanf("%d%d", &N, &K);
	init();
	rep(i, 1, N) scanf("%d", &a[i]), b[i] = 1;
	rep(i, 1, p[0]) {
		int wty = 1, ok = 1;
		rep(j, 1, K) {
			wty *= p[i];
			if (wty >= 100000) { ok = 0; break; }
		}
		if (!ok) break;
		rep(j, 1, N) {
			while (!(a[j] % wty)) a[j] /= wty;
			if (!(a[j] % p[i])) b[j] *= p[i];
		}
	}
	rep(j, 1, N) hash[a[j]]++;
	ll ans = 0;
	rep(j, 1, N) {
		b[j] = b[j]*b[j]*b[j];
		ll val = b[j] / a[j];
		if (b[j] != val * a[j] || val >= 100000) continue;
		ans += hash[val];
		if (val == a[j]) ans--;
	}
	printf("%lld", ans/2);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, b) for (int i = a, end = b; i <= end; i++)
#define repd(i, a, b) for (int i = a, end = b; i >= end; i--)
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define INF (1<<30)

const int maxn = 100000 + 5;

int N, K;
int a[maxn], Hash[maxn];
ll b[maxn];

int p[maxn], check[maxn];
void init() {
	rep(i, 2, 100000) {
		if (!check[i]) p[++p[0]] = i;
		for (register int j = 1; j <= p[0] && i * p[j] <= 100000; j++) {
			check[i*p[j]] = 1;
			if (!(i % p[j])) break;
		}
	}
}

int main() {
	scanf("%d%d", &N, &K);
	init();
	rep(i, 1, N) scanf("%d", &a[i]), b[i] = 1;
	rep(i, 1, p[0]) {
		ll wty = 1;
		int ok = 1;
		rep(j, 1, K) {
			wty *= p[i];
			if (wty > 100000) { ok = 0; break; }
		}
		if (!ok) break;
		rep(j, 1, N)
			while (!(a[j] % wty)) a[j] /= wty;
	}
	rep(i, 1, N) {
		int x = a[i], pt = 1;
		if (x == 1) continue;
		while (check[x]) {
			if (!(x % p[pt])) {
				x /= p[pt];
				if (b[i] % p[pt]) b[i] *= p[pt];
			}
			else pt++;
		}
		if (b[i] % x) b[i] *= x;
	}
	rep(j, 1, N) Hash[a[j]]++;
	ll ans = 0;
	rep(j, 1, N) {
		ll wty = 1, ok = 1;
		rep(i, 1, K) {
			wty = wty * b[j];
			if (wty > 1e10) { ok = 0; break; }
		}
		if (!ok) break;
		ll val = wty / a[j];
		if (val > 100000) continue;
		ans += Hash[val];
		if (val == a[j]) ans--;
	}
	printf("%lld", ans/2);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, b) for (int i = a, end = b; i <= end; i++)
#define repd(i, a, b) for (int i = a, end = b; i >= end; i--)
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define INF (1<<30)

const int maxn = 100000 + 5;

int N, K;
int a[maxn], Hash[maxn];
ll b[maxn];

int p[maxn], check[maxn];
void init() {
	rep(i, 2, 100000) {
		if (!check[i]) p[++p[0]] = i;
		for (register int j = 1; j <= p[0] && i * p[j] <= 100000; j++) {
			check[i*p[j]] = 1;
			if (!(i % p[j])) break;
		}
	}
}

int main() {
	scanf("%d%d", &N, &K);
	init();
	rep(i, 1, N) scanf("%d", &a[i]), b[i] = 1;
	rep(i, 1, p[0]) {
		ll wty = 1;
		int ok = 1;
		rep(j, 1, K) {
			wty *= p[i];
			if (wty > 100000) { ok = 0; break; }
		}
		if (!ok) break;
		rep(j, 1, N)
			while (!(a[j] % wty)) a[j] /= wty;
	}
	rep(i, 1, N) {
		int x = a[i], pt = 1;
		if (x == 1) continue;
		while (check[x]) {
			if (!(x % p[pt])) {
				x /= p[pt];
				if (b[i] % p[pt]) b[i] *= p[pt];
			}
			else pt++;
		}
		if (b[i] % x) b[i] *= x;
	}
	rep(j, 1, N) Hash[a[j]]++;
	ll ans = 0;
	rep(j, 1, N) {
		ll wty = 1, ok = 1;
		rep(i, 1, K) {
			wty = wty * b[j];
			if (wty > 1e10) { ok = 0; break; }
		}
		if (!ok) continue;
		ll val = wty / a[j];
		if (val > 100000) continue;
		ans += Hash[val];
		if (val == a[j]) ans--;
	}
	printf("%lld", ans/2);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i, a, b) for (int i = a, end = b; i <= end; i++)
#define repd(i, a, b) for (int i = a, end = b; i >= end; i--)
#define chkmax(a, b) a = max(a, b)
#define chkmin(a, b) a = min(a, b)
#define INF (1<<30)

const int maxn = 100000 + 5;

int N, K;
int a[maxn], b[maxn], Hash[maxn];

int p[maxn], check[maxn];
void init() {
	rep(i, 2, 100000) {
		if (!check[i]) p[++p[0]] = i;
		for (register int j = 1; j <= p[0] && i * p[j] <= 100000; j++) {
			check[i*p[j]] = 1;
			if (!(i % p[j])) break;
		}
	}
}

int main() {
	scanf("%d%d", &N, &K);
	init();
	rep(i, 1, N) scanf("%d", &a[i]), b[i] = 1;
	rep(i, 1, p[0]) {
		int wty = 1, ok = 1;
		rep(j, 1, K) {
			wty *= p[i];
			if (wty >= 100000) { ok = 0; break; }
		}
		if (!ok) break;
		rep(j, 1, N) {
			while (!(a[j] % wty)) a[j] /= wty;
			if (!(a[j] % p[i])) b[j] *= p[i];
		}
	}
	rep(j, 1, N) Hash[a[j]]++;
	ll ans = 0;
	rep(j, 1, N) {
		b[j] = b[j]*b[j]*b[j];
		ll val = b[j] / a[j];
		if (b[j] != val * a[j] || val >= 100000) continue;
		ans += Hash[val];
		if (val == a[j]) ans--;
	}
	printf("%lld", ans/2);
	return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
using namespace std;
ll N, K, A[101010];
ll mk[101010],mk2[101010],mk3[101010];
vector<ll> pr[100010];
ll i,j,k,ans;
int main(){
    scanf("%lld%lld", &N,&K);
    fornum(i,0,N){
        scanf("%lld", &A[i]);
        mk[A[i]]++;
    }
    fornum(i,1,100010){
        ll kk = K;
        ll a = 1;
        while(kk--&&a<1e11){
            a *= i;
        }
        if(a>=1e11)
            break;
        mk2[i] = a;
    }
    fornum(i,2,100010){
        if(mk3[i])
            continue;
        pr[i].push_back(i);
        for (j = i+i; j < 100010;j+=i){
            pr[j].push_back(i);
            mk3[j] = 1;
        }
    }
    fornum(i,1,100010){
    	if(!mk[i])continue;
        //printf("a");
        ll a = 1;
        ll ii = i;
        fornum(j,0,pr[i].size()){
            ll p = pr[i][j];
            ll k = 0;
            while(ii%p==0){
                ii /= p;
                k++;
            }
            k = (K - k % K) % K;
            while(k--&&a<1e6){
                a *= p;
            }
            if(a>=1e6)
                break;
        }
        //printf("%lldb", a);
        j = 1;
        while(a*mk2[j]<=i){
            if(a*mk2[j]==i){
                ans += mk[i] * (mk[i] - 1) / 2;
            }else{
                ans += mk[i] * mk[a * mk2[j]];
            }
            j++;
        }
        //printf("%lld,%lld ", i, ans);
    }
    printf("%lld", ans);

    return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
using namespace std;
ll N, K, A[101010];
ll mk[101010],mk2[101010],mk3[101010];
vector<ll> pr[100010];
ll i,j,k,ans;
int main(){
    scanf("%lld%lld", &N,&K);
    fornum(i,0,N){
        scanf("%lld", &A[i]);
        mk[A[i]]++;
    }
    fornum(i,1,100010){
        ll kk = K;
        ll a = 1;
        while(kk--&&a<1e11){
            a *= i;
        }
        if(a>=1e11)
            break;
        mk2[i] = a;
    }
    fornum(i,2,100010){
        if(mk3[i])
            continue;
        pr[i].push_back(i);
        for (j = i; j < 100010;j+=i){
            mk3[j] = 1;
        }
    }
    fornum(i,1,100010){
    	if(!mk[i])continue;
        //printf("a");
        ll a = 1;
        ll ii = i;
        fornum(j,0,pr[i].size()){
            ll p = pr[i][j];
            ll k = 0;
            while(ii%p==0){
                ii /= p;
                k++;
            }
            k = (K - k % K) % K;
            while(k--&&a<1e6){
                a *= p;
            }
            if(a>=1e6)
                break;
        }
        //printf("%lldb", a);
        j = 1;
        while(a*mk2[j]<=i){
            if(a*mk2[j]==i){
                ans += mk[i] * (mk[i] - 1) / 2;
            }else{
                ans += mk[i] * mk[a * mk2[j]];
            }
            j++;
        }
        //printf("%lld,%lld ", i, ans);
    }
    printf("%lld", ans);

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5;
const int MAX = 1e5+10;
vector<ll>prime[MAX];
ll a[MAX],cnt[MAX];
ll powk[MAX],flag[MAX];
int main(){
	ll n,k;
	scanf("%lld%lld",&n,&k);
	for(ll i = 1; i <= n; i++) scanf("%lld",&a[i]),cnt[a[i]]++;
	for(ll i = 1; i <= N; i++){
		ll p = k,a = 1;
		while(p--&&a<=1e10) a*=i;
		if(a>1e10) break;
		powk[i] = a;
	}
	for(ll i = 2; i <= N; i++){
		if(flag[i]) continue;
		prime[i].push_back(i);
		for(ll j = i+i; j <= N; j+=i){
			flag[j] = 1;
			prime[j].push_back(i);
		}
	}
	ll ans = 0;
	for(ll i = 1; i <= N; i++){
		if(!cnt[i])continue;
		ll a = 1, p = i;
		for(auto v:prime[i]){
			ll o = 0;
			while(p%v==0) p/=v,o++;
			o = (k-o%k)%k;
			while(o--&&a<=N+1) a*=v;
			if(a>N+1) break;
		}
		int st = 1;
		while(a*powk[st]<=i){
			if(a*powk[st]==i) ans+=cnt[i]*(cnt[i]-1)/2;
			else ans+=cnt[i]*cnt[a*powk[st]];
			st++;
		}
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e5;
const int MAX = 1e5+10;
vector<ll>prime[MAX];
ll a[MAX],cnt[MAX];
ll powk[MAX],flag[MAX];
int main(){
	ll n,k;
	scanf("%lld%lld",&n,&k);
	for(ll i = 1; i <= n; i++) scanf("%lld",&a[i]),cnt[a[i]]++;
	for(ll i = 1; i <= N; i++){
		ll p = k,a = 1;
		while(p--&&a<=1e10) a*=i;
		if(a>1e10) break;
		powk[i] = a;
	}
	for(ll i = 2; i <= N; i++){
		if(flag[i]) continue;
		prime[i].push_back(i);
		for(ll j = i+i; j <= N; j+=i){
			flag[j] = 1;
			prime[j].push_back(i);
		}
	}
	ll ans = 0;
	for(ll i = 1; i <= N; i++){
		if(!cnt[i])continue;
		ll a = 1, p = i;
		for(auto v:prime[i]){
			ll o = 0;
			while(p%v==0) p/=v,o++;
			o = (k-o%k)%k;
			while(o--&&a<=N+1) a*=v;
			if(a>N+1) break;
		}
	//	printf("i = %lld a = %lld",i,a);
		int st = 1;
		while(a*powk[st]<=i){
			if(a*powk[st]==i) ans+=cnt[i]*(cnt[i]-1)/2;
			else ans+=cnt[i]*cnt[a*powk[st]];
			st++;
		}
	//	printf(" ans = %d\n",ans);
	}
	printf("%lld\n",ans);
	return 0;
}
/*
2 3
216 1
*/


#include<bits/stdc++.h>

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
using namespace std;
ll N, K, A[101010];
ll mk[101010],mk2[101010],mk3[101010];
vector<ll> pr[100010];
ll i,j,k,ans;
int main(){
    scanf("%lld%lld", &N,&K);
    fornum(i,0,N){
        scanf("%lld", &A[i]);
        mk[A[i]]++;
    }
    fornum(i,1,100010){
        ll kk = K;
        ll a = 1;
        while(kk--&&a<1e11){
            a *= i;
        }
        if(a>=1e11)
            break;
        mk2[i] = a;
    }
    fornum(i,2,100010){
        if(mk3[i])
            continue;
        pr[i].push_back(i);
        for (j = i; j < 100010;j+=i){
            pr[j].push_back(i);
            mk3[j] = 1;
        }
    }
    fornum(i,1,100010){
    	if(!mk[i])continue;
        //printf("a");
        ll a = 1;
        ll ii = i;
        fornum(j,0,pr[i].size()){
            ll p = pr[i][j];
            ll k = 0;
            while(ii%p==0){
                ii /= p;
                k++;
            }
            k = (K - k % K) % K;
            while(k--&&a<1e6){
                a *= p;
            }
            if(a>=1e6)
                break;
        }
        //printf("%lldb", a);
        j = 1;
        while(a*mk2[j]<=i){
            if(a*mk2[j]==i){
                ans += mk[i] * (mk[i] - 1) / 2;
            }else{
                ans += mk[i] * mk[a * mk2[j]];
            }
            j++;
        }
        //printf("%lld,%lld ", i, ans);
    }
    printf("%lld", ans);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define lp(i, a, b) for (int i=a; i<(b); i++)
#define lpd(i,a,b) for (int i = (b)-1; i >= a; i--)

#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;

int n, k;

vector<pair<int, int> > decompose(int n){
    vector<pair<int, int > > v;
    for (int i=2;i*i<=n;++i){
        int cnt = 0;
        while (n%i == 0){
            cnt++;
            n/=i;
        }
        if (cnt%k > 0) v.pb({i, cnt%k});
    }
    if (n > 1) v.push_back({n, 1});
    return v;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>k;
    int a;
    map<vector<pair<int, int> >, int > mp;
    ll ans = 0;
    lp(i, 0, n) {
        cin>>a;
        vector<pair<int, int> > v = decompose(a);
        vector<pair<int, int> > v1 = v;
        int len = sz(v1);
        lp(i, 0, len) v[i].second = k - v[i].second;
        debug(a, v1, mp[v1]);
        ans += (ll)mp[v1];
        mp[v]++;
    }
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define lp(i, a, b) for (int i=a; i<(b); i++)
#define lpd(i,a,b) for (int i = (b)-1; i >= a; i--)

#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;

vi primeDivisors;
int n, k;
const int NMAX = 1e5+1;

void sieve(){
    for (int i=2;i*i<=NMAX;++i){
        if (primeDivisors[i] == -1){
            for (int j=i*i;j<=NMAX;j+=i){
                primeDivisors[j] = i;
            }
        }
    }
    for (int i=1;i<=NMAX;++i)
        if (primeDivisors[i] == -1)
            primeDivisors[i] = i;
}

vector<pair<int, int> > decompose (int n) {
    vector<pair<int, int> > v;
    while (n > 1){
        int cnt = 0;
        int d = primeDivisors[n];
        while (n%d == 0) {
            cnt++;
            n /= d;
        }
        if (cnt % k > 0 ) v.pb({d, cnt%k});
    }
    return v;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>k;
    primeDivisors = vi(NMAX, -1);
    sieve();
    int a;
    map<vector<pair<int, int> > , int> mp;
    ll ans = 0;
    lp(i, 0, n){
        cin>>a;
        vector<pair<int, int> > v = decompose(a);
        vector<pair<int, int> > v1 = v;
        int len =sz(v1);

        for (int i=0;i<len;++i)
            v1[i].second = k - v1[i].second;

        ans += (ll)mp[v1];
        mp[v]++;
    }
    cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define lp(i, a, b) for (int i=a; i<(b); i++)
#define lpd(i,a,b) for (int i = (b)-1; i >= a; i--)

#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;

vi primeDivisors;
int n, k;
const int NMAX = 1e5+1;

void sieve() {
    for (int i=2;i*i<=NMAX;++i){
        if (primeDivisors[i] == -1){
            for (int j=i*i;j<=NMAX;j+=i){
                primeDivisors[j] = i;
            }
        }
    }
    for (int i=1;i<=NMAX;++i)
        if (primeDivisors[i] == -1)
            primeDivisors[i] = i;
}

vector<pair<int, int> > decompose(int n) {
    vector<pair<int, int> > v;
    while (n > 1) {
        int cnt = 0;
        int d = primeDivisors[n];
        while (n%d == 0) {
            cnt++;
            n /= d;
        }
        if (cnt % k > 0 ) v.pb({d, cnt%k});
    }
    return v;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin>>n>>k;
    primeDivisors = vi(NMAX+1, -1);
    sieve();
    int a;
    map<vector<pair<int, int> > , int> mp;
    ll ans = 0;
    lp(i, 0, n){
        cin>>a;
        vector<pair<int, int> > v = decompose(a);
        vector<pair<int, int> > v1 = v;
        debug(a, v, v1);
        int len =sz(v1);

        for (int i=0;i<len;++i)
            v1[i].second = k - v1[i].second;

        ans += (ll)mp[v1];
        mp[v]++;
    }
    cout<<ans<<endl;

    return 0;
}