#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll N = 5e5 + 5;
const ll NN = 5e3 + 5;
const ll INF = 2e18 + 5;
const ll MOD = 1e9 + 7;
const ll P = 53;
const double eps = 1e-9;
ll dp[NN][NN], a[N];
void solve() {
ll n, k;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
a[0] = a[1] - 1;
for(ll i = 1; i <= n; i++) {
ll x = min(i, k);
for(int j = 1; j <= x; j++) {
dp[i][j] = dp[i - 1][j] + (a[i] - a[i - 1]);
}
if(a[i - 1] + 1 != a[i]) {
for(int j = 1; j <= x; j++) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 2);
}
}
}
cout << dp[n][k] << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int test = 1;
// cin >> test;
for(int i = 1; i <= test; i++) {
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... |