This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;
int n, k;
int t[100005];
int ds[100005];
int dp[5005][5005];
int solve(int idx, int kLeft) {
if (kLeft < 0) return 1000000000;
if (idx >= n - 1) return 0;
if (dp[idx][kLeft] != -1) return dp[idx][kLeft];
return dp[idx][kLeft] = min(ds[idx] + solve(idx + 1, kLeft), solve(idx + 1, kLeft - 1));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
FOR (i, n) {
cin >> t[i];
}
FOR (i, n - 1) {
ds[i] = t[i+1] - t[i] - 1;
}
memset(dp, -1, sizeof(dp));
cout << n + solve(0, k - 1) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |