#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, a;
int cost = 1;
vector<int> v;
cin >> n >> k;
for ( int i = 1; i <= n; i++ ) {
int b;
cin >> b;
if ( i > 1 ) {
v.push_back( b - a - 1 );
cost += b - a;
}
a = b;
}
sort( v.begin(), v.end() );
reverse( v.begin(), v.end() );
for ( int i = 0; i < k - 1; i++ )
cost -= v[i];
cout << cost;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |