#include #include #include #include #include #include using namespace std; struct UnionFind { int n; vector parent, siz; UnionFind(int n) : n(n) { parent.resize(n); siz.resize(n); for (int i = 0; i < n; ++i) { parent[i] = i; siz[i] = 1; } } int root(int x) { if (parent[x] == x) return x; return parent[x] = root(parent[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); parent[y] = x; siz[x] += siz[y]; return true; } int same(int x, int y) { return root(x) == root(y); } int size(int x) { return siz[root(x)]; } }; using ll = long long; struct RminQ { const ll INFLL = 1e18; int m; vector dat; RminQ(int n) { m = 1; while (m < n) m *= 2; dat.assign(2 * m - 1, INFLL); } void update(int i, long long x) { i += m - 1; dat[i] = x; while (i) { i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } // [a, b) long long query(int a, int b, int k, int l, int r) { if (l >= b || r <= a) return INFLL; if (a <= l && r <= b) return dat[k]; long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2); long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } long long query_sub(int a, int b) { return query(a, b, 0, 0, m); } }; int main(void) { int n, k; cin >> n >> k; vector a(n + 2, 0); for (int i = 1; i <= n; ++i) cin >> a[i]; RminQ seg(n + 5); seg.update(0, 0); for (int i = 1; i <= n + 1; ++i) { long long mn = seg.query_sub(max(i - k, 0), i); seg.update(i, mn + a[i]); } long long ans = 0; for (int i = 0; i < n + 2; ++i) ans += a[i]; ans -= seg.query_sub(n+1, n+2); cout << ans << endl; return 0; }