#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define pi pair<int, int>
#define FOR(i,a,b) for(int i = a; i <= b; i ++)
#define REP(i,a,b) for(int i = a; i >= b; i --)
#define el "\n"
#define bit(mask,i) ((mask >> i) & 1)
#define all(x) (x).begin(),(x).end()
#define int long long
#define vll vector<int>
#define NAME ""
using namespace std;
const int INF = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 5e3 + 5;
void maximize (int &x, int y) {
x = max (x, y);
}
void minimize (int &x, int y) {
x = min (x, y);
}
//-------------------
int n, k, a[MAXN], dp[MAXN][MAXN];
void solve () {
cin >> n >> k;
FOR (i, 1, n) cin >> a[i];
sort (a + 1, a + 1 + n);
vll vec;
int res = a[n] - a[1] + 1;
k --;
FOR (i, 2, n) vec.pb (a[i] - a[i - 1] - 1);
sort (all (vec), greater <int>());
FOR (i, 1, k) res -= vec[i - 1];
cout << res;
}
signed main () {
ios_base::sync_with_stdio(0); cin.tie (0);
// freopen (NAME".INP", "r", stdin);
// freopen (NAME".OUT", "w", stdout);
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... |