#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
int dp[n + 1][5001];
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= 5000; j++) {
dp[i][j] = n + 1;
}
}
for(int j = 1; j <= k; j++) {
dp[1][j] = (a[1] != j);
}
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= 5000; j++) {
for(int q = 1; q <= 5000; q++) {
if(j - q <= k) {
dp[i][j] = min(dp[i][j], dp[i - 1][q] + (a[i] != j));
}
}
}
}
int ans = n + 1;
for(int i = 1; i <= 5000; i++) {
ans = min(ans, dp[n][i]);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |