This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<ll,ll> ii;
const ll maxn=300010;
ll n,p[maxn],a[maxn];
priority_queue<ii,vector<ii>,greater<ii>> q;
int root(int i)
{
if (p[i] == i) return i;
return root(p[i]);
}
void merge (long i,long j)
{
p[root(i)]=p[root(j)];
}
ll b[maxn],k;
int main()
{
ios_base :: sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>k;
for (ll i=1;i<=n;i++) cin>>a[i];
sort (a+1,a+n+1);
for (ll i=1;i<n;i++) b[i]=a[i+1]-a[i];
ll res=0;
n--;
sort (b+1,b+n+1);
for (ll i=1;i<=n-k+1;i++)
res=res+b[i];
res+=k;
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |