Pagini recente » Cod sursa (job #1397623) | Cod sursa (job #2970402) | Cod sursa (job #1891781) | Borderou de evaluare (job #2080147) | Cod sursa (job #2650777)
#include <bits/stdc++.h>
using namespace std;
int main () {
ifstream fin ("deque.in");
ofstream fout ("deque.out");
fin.sync_with_stdio(false);
fout.sync_with_stdio(false);
fin.tie(nullptr);
fout.tie(nullptr);
int n, k;
fin >> n >> k;
deque <pair <int, int>> DQ;
long long ans = 0;
int i, x;
for (i = 1; i <= n; i++) {
fin >> x;
while (!DQ.empty() && DQ.back().second >= x)
DQ.pop_back();
DQ.emplace_back(i, x);
if (DQ.front().first == i - k)
DQ.pop_front();
if (i >= k)
ans += DQ.front().second;
}
fout << ans;
return 0;
}