Pagini recente » Clasamentul arhivei de probleme | Cod sursa (job #2668399) | Cod sursa (job #2668291) | Clasamentul arhivei Infoarena Monthly | Cod sursa (job #2668187)
#include <bits/stdc++.h>
using namespace std;
int main() {
// freopen("deque.in", "r", stdin);
// freopen("deque.out", "w", stdout);
int n, k;
scanf("%d%d", &n, &k);
int ql = 0;
int st = 0;
const int mxN = 5e6 + 1;
int* a = new int[mxN];
int* q = new int[mxN];
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
auto push = [&] (int el) {
while (ql > st && el <= q[ql-1]) {
ql--;
}
q[ql++] = el;
};
for (int i = 0; i < k; i++) {
push(a[i]);
}
int res = q[st];
for (int i = k; i < n; i++) {
if (q[st] == a[i-k]) st++;
push(a[i]);
res += q[st];
}
printf("%d", res);
}