#include <bits/stdc++.h>
using namespace std;
#define w first
#define h second
#define pb push_back
#define int long long
const int N = 5e5+100;
int n, m;
int a[5100], dp[5100][5100];
void solve(){
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= 5000; j++) dp[i][j]=1e18;
}
dp[0][0] = 0;
int sf[5000 + 2];
sf[5000 + 1] = 1e18;
for(int i = 1; i <= n; i++){
for(int j = max(0ll, a[i] - m); j <= 5000; j++) dp[i][a[i]] = min(dp[i][a[i]], dp[i-1][j]);
for(int j = 5000; j >= 0; j--) sf[j] = min(sf[j + 1], dp[i - 1][j]);
for(int j = 0; j <= 5000; j++){
dp[i][j] = min(dp[i][j], sf[max(0ll, j-m)] + 1);
}
sf[5000 + 1] = 1e18;
}
int ans = 1e18;
for(int i = 0; i <= 5000; i++) ans = min(ans, dp[n][i]);
cout << ans << '\n';
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("promote.in","r",stdin);
//freopen("promote.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... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |