constint maxn = 100005; int n, k, f[maxn], a[maxn]; structnode {int val, pos; } q[maxn];
signedmain(){ cin >> n >> k; f[0] = 0; int head = 0, tail = 1, sum = 0; // min -> increase q[head] = (node) { f[0], 0 }; for (int i = 1; i <= n; i ++) { cin >> a[i]; sum += a[i]; f[i] = a[i] + q[head].val; while (head < tail && f[i] <= q[tail - 1].val) tail --; q[tail ++] = (node) { f[i], i }; while (head < tail && i - q[head].pos + 1 > k + 1) head ++; } // for (int i = 0; i <= n; i ++) cout << f[i] << " "; // cout << endl; int ans = 8e18; for (int i = n - k; i <= n; i ++) ans = min(ans, f[i]); cout << sum - ans << endl; return0; }