#include <bits/stdc++.h>   using namespace std;   template <typename T>   struct Segtree {     int n;     T e;     vector<T> dat;     typedef function<T(T a, T b)> Func;     Func f;     Segtree() {}     Segtree(int n_input, Func f_input, T e_input) {       initialize(n_input, f_input, e_input);     }     void initialize(int n_input, Func f_input, T e_input) {       f = f_input;       e = e_input;       n = 1;       while (n < n_input) n <<= 1;       dat.resize(2 * n - 1, e);     }     void update(int k, T a) {       k += n - 1;       dat[k] = a;       while (k > 0) {         k = (k - 1) / 2;         dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);       }     }     T get(int k) { return dat[k + n - 1]; }     T between(int a, int b) { return query(a, b + 1, 0, 0, n); }     T query(int a, int b, int k, int l, int r) {       if (r <= a || b <= l) return e;       if (a <= l && r <= b) return dat[k];       T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);       T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);       return f(vl, vr);     }   };   void chmin(int64_t& a, int64_t b) { a = min(a, b); }   int main() {     int N, C, A[100000];     cin >> N >> C;     for (int i = 0; i < N; i++) cin >> A[i];     Segtree<int> st(         N, [](int a, int b) { return min(a, b); }, 1e9);     Segtree<int64_t> sum(         N, [](int64_t a, int64_t b) { return a + b; }, 0);     for (int i = 0; i < N; i++) {       st.update(i, A[i]);       sum.update(i, A[i]);     }     vector<int64_t> dp(N + 1, 1e18);     dp[0] = 0;     for (int i = 0; i < N; i++) {       chmin(dp[i + 1], dp[i] + A[i]);       if (i + C <= N)         chmin(dp[i + C],               dp[i] + sum.between(i, i + C - 1) - st.between(i, i + C - 1));     }     cout << dp[N] << endl;     return 0;   }   