#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define F first
#define S second
const int N = 5000 + 5, inf = 1e9 + 1, mod = 1e9 + 7;
int n, k, a[N], dp[N][N];
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i <= n; i++){
for(int j = 0; j <= k; j++) dp[i][j] = 1e16;
}
dp[0][0] = 0;
a[0] = 0;
for(int i = 1; i <= n; i++){
int cur = 1;
for(int j = i - 1; j >= 0; j--){
for(int l = 0; l < min(j + 1, k); l++){
dp[i][l + 1] = min(dp[i][l + 1], dp[j][l] + cur);
}
cur = a[i] - a[j] + 1;
}
}
//for(int i = 1; i <= n; i++){
// cout << dp[i][0] << ' ' << dp[i][1] << ' ' << dp[i][2] << '\n';
//}
cout << dp[n][k] << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("guard.in", "r", stdin);
//freopen("guard.out", "w", stdout);
int tt = 1;
//cin >> tt;
while(tt--) solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |