#include #define endl '\n' using namespace std; const int N = 10007; const int K = 1007; const long long INF = (1e18) + 7; int tests,current_case; int n,k,a[N]; bool used[N][K]; long long state[N][K]; long long ans; void message(int current_case) { //cerr<<"Case "<n) { if(n-last<=k) return 0; else return INF; } if(used[pos][last]) return state[pos][last]; long long ans=recurse(pos+1,pos)+a[pos]; if(pos==n) { if(n-last<=k) ans=min(ans,recurse(pos+1,last)); } else { if(pos+1-k<=last+k) ans=min(ans,recurse(pos+1,last)); } used[pos][last]=true; state[pos][last]=ans; return ans; } int main() { //ios_base::sync_with_stdio(false); //cin.tie(NULL); int i; tests=1; //scanf("%d", &tests); for(current_case=1;current_case<=tests;current_case++) { scanf("%d %d", &n, &k); for(i=1;i<=n;i++) scanf("%d", &a[i]); if(k>=n-1) { ans=INF; for(i=1;i<=n;i++) ans=min(ans,(long long)(a[i])); printf("%lld\n", ans); goto STOP; } memset(used,0,sizeof(used)); ans=INF; for(i=1;i<=k+1 && i<=n;i++) ans=min(ans,recurse(i+1,i)+a[i]); //ans=min(ans,recurse(1,0)); printf("%lld\n", ans); STOP: message(current_case); } return 0; }