#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define endl '\n'
#define pb(x) push_back(x)
const int MOD = 1e9+7;
const int inf =1e9;
const ll INF = 1e15;
const ll INV2 = 500000004;
class DSU {
vector<int> parent, size;
public:
DSU(int n) {
parent.resize(n+1);
size.resize(n+1);
for (int i=0; i<=n; i++) {
parent[i]=i;
size[i]=1;
}
}
int find (int node) {
if (node==parent[node]) return node;
return parent[node]=find(parent[node]);
}
void unite (int u, int v) {
int pv=find(v);
int pu=find(u);
if (pv==pu) return;
if (size[pu]<size[pv]) swap(pu, pv);
parent[pv]=pu;
size[pu]+=size[pv];
}
int getsize (int node) {
return size[find(node)];
}
};
ll getsqrt(ll x) {
ll val = sqrtl(x) + 2;
while (val * val > x) val--;
return val;
}
bool safe (int i, int j, int n, int m) {
if (i<0 || j<0 || i>=n || j>=m) return false;
return true;
}
vector<int> dx={-1, 0, 1, 0};
vector<int> dy={0, 1, 0, -1};
// SOLUTION STARTS FROM HERE //
void solve() {
int n, k;
cin>>n>>k;
vector<int> a(n);
for (int i=0; i<n; i++) {
cin>>a[i];
}
int t=a[n-1]-a[0]+1;
vector<int> g;
for (int i=1; i<n; i++) {
g.push_back(a[i]-a[i-1]-1);
}
sort(g.rbegin(), g.rend());
k--;
int i=0;
while (k--) {
t-=g[i];
i++;
}
cout<<t<<endl;
}
bool multi=false;
int main()
{
//freopen("snakes.in", "r", stdin);
//freopen("snakes.out", "w", stdout);
int t=1;
if (multi) cin>>t;
while (t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |