#include using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N, K; LL A[200000]; int P[18][200000] = {}; LL S[18][200000] = {}; LL getsum(int p, int q) { LL ans = 0; for (int d = 17; d >= 0; d--) { if (q < (1 << d)) continue; ans += S[d][p]; p = P[d][p]; q -= (1 << d); } return ans; } int main() { cin >> N >> K; rep(i, N) cin >> A[i]; deque wnd; rep(i, N) { if (i >= K) P[0][i] = wnd.front(); while (wnd.size()) { if (A[wnd.back()] < A[i]) break; wnd.pop_back(); } wnd.push_back(i); if (wnd.front() + K <= i) wnd.pop_front(); } rep(d, 17) rep(i, N) P[d + 1][i] = P[d][P[d][i]]; rep(i, N) S[0][i] = A[i]; rep(d, 17) rep(i, N) S[d + 1][i] = S[d][i] + S[d][P[d][i]]; LL ans = 1000000000000000000; for (int i = 1; i * K <= N; i++) ans = min(ans, getsum(i * K - 1, i)); cout << ans << endl; return 0; }