#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } struct UnionFind{ private: vector par,siz; public: int con; UnionFind(int n):par(n),siz(n,1),con(n){ iota(begin(par),end(par),0); } int root(int x){ return (par[x]==x?x:(par[x]=root(par[x]))); } bool sameroot(int x,int y){ return root(x)==root(y); } bool unite(int x,int y){ x=root(x);y=root(y); if(x==y) return false; if(siz[x]>n>>k; vector v(n); cin>>v; sort(ALL(v)); using T=tuple; vector e; rep(i,n-1) e.emplace_back(v[i+1]-v[i],i+1,i); sort(ALL(e)); UnionFind uf(n); for(int i=0;uf.con>k;i++){ auto [_, a, b]=e[i]; uf.unite(a,b); } vector mi(n,LINF),ma(n,-LINF); rep(i,n)chmin(mi[uf.root(i)],v[i]),chmax(ma[uf.root(i)],v[i]); ll res=0; rep(i,n)if(i==uf.root(i)) res+=ma[i]-mi[i]; cout<