用优先队列存放不连续的断点及断的位置。优先取间距大的,在断点断开。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,INF=0x7FFFFFFF;
lon arr[SZ],cut[SZ]; struct nd{
int a,b;
nd(int x,int y):a(x),b(y){}
bool operator<(const nd &rbs)const
{
return a<rbs.a;
}
}; int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
//cin>>casenum;
//for(lon time=1;time<=casenum;++time)
{
lon k,tot,n;
cin>>k>>tot>>n;
priority_queue<nd> pq;
for(int i=;i<=n;++i)
{
cin>>arr[i];
}
sort(arr+,arr++n);
for(int i=;i<=n;++i)
{
if(i!=&&arr[i]-arr[i-]>)
{
pq.push(nd(arr[i]-arr[i-],i));
}
}
for(;k!=;)
{
if(pq.empty())--k;
else
{
nd pos=pq.top();
pq.pop();
//cout<<"pos.a: "<<pos.a<<endl;
cut[pos.b]=;
--k;
}
}
int bg=;
int res=;
for(int i=;i<=n;++i)
{
if(cut[i])
{
//cout<<"i: "<<i<<endl;
res+=arr[i-]-arr[bg]+;
//cout<<(arr[i-1]-arr[bg]+1)<<endl;
bg=i;
}
}
res+=arr[n]-arr[bg]+;
cout<<res<<endl;
}
return ;
}