cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i], s[i] = s[i - 1] + a[i]; for (int i = 1; i < m; ++i) { while (h <= t && a[i] <= a[Q[t]]) --t; Q[++t] = i; } for (int i = m; i <= n; ++i) { while (h <= t && a[i] <= a[Q[t]]) --t; Q[++t] = i; while (h < t && i - Q[h] + 1 > m) ++h; b[i] = a[Q[h]]; } for (int i = 1; i <= n; ++i) { f[i] = f[i - 1] + a[i]; if (i - m >= 0) f[i] = min(f[i], f[i - m] + s[i] - s[i - m] - b[i]); } cout << f[n] << "\n"; return0; }