#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,k,a[N];
typedef pair<ll,int> pr;
priority_queue<pr,vector<pr>,greater<pr> > Q;
int main()
{
freopen("section_monitor.in","r",stdin);
freopen("section_monitor.out","w",stdout);
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+n+1);
Q.push(pr(0,0));
for (int i=0;i<=k;i++){
pr now=Q.top();Q.pop();
if (i==k){printf("%lld\n",now.first);break;}
if (now.second==n) continue;
now.first+=a[++now.second];
Q.push(now);
if (now.second==1) continue;
now.first-=a[now.second-1];
Q.push(now);
}
return 0;
}