#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 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);
FOR (i, 1, n) FOR (j, 0, k) dp[i][j] = INF;
a[0] = a[1] - 1;
FOR (i, 1, n) {
FOR (j, 1, k) {
dp[i][j] = dp[i - 1][j] + a[i] - a[i - 1];
minimize (dp[i][j], dp[i - 1][j - 1] + 1);
// cout << i << " " << j << " " << dp[i][j] << el;
}
}
cout << dp[n][k];
}
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... |