Pagini recente » Cod sursa (job #1055866) | Agm 2018 Runda 2 | Cod sursa (job #1882579) | Cod sursa (job #2073009) | Cod sursa (job #3121279)
#include <bits/stdc++.h>
using namespace std;
fstream f("deque.in", ios::in), g("deque.out", ios::out);
int main()
{
int n, k, x;
long long ans = 0;
deque<pair<int, int>> q;
f >> n >> k;
for (int i = 1; i <= k; i++)
{
f >> x;
while (!q.empty() && q.back().first > x)
q.pop_back();
q.push_back({x, i});
}
ans += q.front().first;
for (int i = k + 1; i <= n; i++)
{
f >> x;
while (!q.empty() && q.back().first > x)
q.pop_back();
while (!q.empty() && q.front().second <= i - k)
q.pop_front();
q.push_back({x, i});
ans += q.front().first;
}
g << ans;
}