#include #define mp make_pair #define PII pair #define fi first #define se second #define pb push_back using namespace std; //******************************************** //Error tracking #define show(args...) { vector _v = split(#args, ','); err(_v.begin(), args); } vector split(const string& s, char c) { vector v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector::iterator it) {} template void err(vector::iterator it, T a, Args... args) { cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << '\n'; err(++it, args...); } //******************************************** const int NMAX = 10005; int n,k; long long dp[NMAX],dp1[NMAX],sol,c[NMAX]; int main() { int i,j; // freopen("input","r",stdin); // freopen("output","w",stdout); cin.sync_with_stdio(false); cin >> n >> k; for (i = 1; i <= n; i++) {cin >> c[i]; dp[i] = dp1[i] = 1LL << 60;} for (i = 1; i <= n && i <= (k + 1); i++) dp[min(n, i + k)] = min(dp[min(n,i + k)], c[i]); for (i = n; i >= 1 && i >= (n - k); i--) dp1[max(1, i - k)] = min(dp1[max(1,i - k)], c[i]); sol = min(dp1[1],dp[n]); //if there's any for (i = 1; i <= n; i++) //now full if ((i - 2*k - 1) >= 0) dp[i] = min(dp[i], c[i - k] + dp[i - 2*k -1]); //for (i = 1; i <= n ; i++) cout << dp[i] << " "; // cout << "\n"; sol = min(sol,dp[n]); //combine for (i = 1; i <= n; i++) sol = min(sol, dp[i] + dp1[i + 1]); cout << sol << "\n"; return 0; }