Pagini recente » Cod sursa (job #2598458) | Cod sursa (job #1561522) | Cod sursa (job #818810) | Cod sursa (job #2508780) | Cod sursa (job #1455393)
//pronuntat "deck" !? ayy lmao
#include <iostream>
#include <fstream>
#define mkpair(a,b) std::make_pair((a), (b))
#define tuple std::pair<int,int>
const char IN[] = "deque.in", OUT[] = "deque.out";
const int NMAX = 5000000;
using namespace std;
tuple deque[NMAX];
int head = -1, tail = -1;
int N, K;
inline void read_data() {
freopen(IN, "r", stdin);
scanf("%d %d", &N, &K);
long long s = 0;
for (int i = 0; i < N; ++i) {
int x;
scanf("%d", &x);
while (tail != -1 && deque[tail].first >= x) --tail;
while (head != -1 && i - deque[head].second >= K) ++head;
if (head > tail) head = tail = -1;
deque[++tail] = mkpair(x, i);
if (head == -1) head = 0;
if (i >= K - 1) {
s += deque[head].first;
}
}
fprintf(fopen(OUT, "w"), "%lld\n", s);
}
int main() {
read_data();
return 0;
}