using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, k) = (c[0], c[1]); var a = NList; WriteLine(Sushi(n, k, a)); } static long Sushi(int n, int k, int[] a) { var set = new AvlTree(); set.Insert(0); var dlist = new long[n + 1]; for (var i = 0; i < n; ++i) { var min = set.ElementAt(0); dlist[i + 1] = min + a[i]; set.Insert(dlist[i + 1]); if (i + 1 - k >= 0) set.Delete(dlist[i + 1 - k]); } var ans = 0L; foreach (var ai in a) ans += ai; return ans - set.ElementAt(0); } public class AvlTree where T: IComparable { private class Node where U: IComparable { public U Value; public Node Lc = null; public Node Rc = null; public int Height; public int Count; public int CCount; public Node(int height, U value) { Height = height; Count = 1; CCount = 1; Value = value; } } private Node root = null; private bool active; static int GetHeight(Node t) { return t == null ? 0 : t.Height; } static int GetCount(Node t) { return t == null ? 0 : t.Count; } static int GetBalance(Node t) { return GetHeight(t.Lc) - GetHeight(t.Rc); } static void Recalc(Node t) { t.Height = 1 + Math.Max(GetHeight(t.Lc), GetHeight(t.Rc)); t.Count = t.CCount + GetCount(t.Lc) + GetCount(t.Rc); } static Node RotateLeft(Node t) { Node u = t.Rc; Node t2 = u.Lc; u.Lc = t; t.Rc = t2; Recalc(u.Lc); Recalc(u); return u; } static Node RotateRight(Node t) { Node u = t.Lc; Node t2 = u.Rc; u.Rc = t; t.Lc = t2; Recalc(u.Rc); Recalc(u); return u; } static Node RotateLR(Node t) { t.Lc = RotateLeft(t.Lc); return RotateRight(t); } static Node RotateRL(Node t) { t.Rc = RotateRight(t.Rc); return RotateLeft(t); } Node BalanceLeft(Node t) { if (!active) return t; var h = GetHeight(t); if (GetBalance(t) > 1) { if (GetBalance(t.Lc) < 0) t = RotateLR(t); else t = RotateRight(t); } Recalc(t); active = h != GetHeight(t); return t; } Node BalanceRight(Node t) { if (!active) return t; var h = GetHeight(t); if (GetBalance(t) < -1) { if (GetBalance(t.Rc) > 0) t = RotateRL(t); else t = RotateLeft(t); } Recalc(t); active = h != GetHeight(t); return t; } public void Insert(T value) { active = false; root = Insert(root, value); } Node Insert(Node cur, T value) { if (cur == null) { active = true; return new Node(1, value); } var d = value.CompareTo(cur.Value); if (d < 0) { cur.Lc = Insert(cur.Lc, value); return BalanceLeft(cur); } else if (d > 0) { cur.Rc = Insert(cur.Rc, value); return BalanceRight(cur); } else { ++cur.CCount; Recalc(cur); return cur; } } public void Delete(T value) { active = false; root = Delete(root, value); } Node Delete(Node cur, T value) { if (cur == null) return null; var d = value.CompareTo(cur.Value); if (d < 0) { cur.Lc = Delete(cur.Lc, value); return BalanceRight(cur); } else if (d > 0) { cur.Rc = Delete(cur.Rc, value); return BalanceLeft(cur); } else { --cur.CCount; if (cur.CCount == 0) { if (cur.Lc != null) { var max = DeleteMax(cur.Lc); cur.Lc = max.keep; cur.Value = max.del.Value; cur.CCount = max.del.CCount; return BalanceRight(cur); } else { active = true; return cur.Rc; } } else { Recalc(cur); return cur; } } } (Node keep, Node del) DeleteMax(Node t) { if (t.Rc == null) { active = true; return (t.Lc, t); } else { var max = DeleteMax(t.Rc); t.Rc = max.keep; return (BalanceLeft(t), max.del); } } public T ElementAt(int pos) { var t = ElementAt(root, pos); if (t == null) return default; return t.Value; } Node ElementAt(Node cur, int pos) { if (pos < GetCount(cur.Lc)) return ElementAt(cur.Lc, pos); if (pos < GetCount(cur.Lc) + cur.CCount) return cur; if (pos < cur.Count) return ElementAt(cur.Rc, pos); return null; } } }