Pagini recente » Cod sursa (job #1855548) | Diferente pentru utilizator/siminescu intre reviziile 2 si 3 | Cod sursa (job #374211) | Diferente pentru problema/cercuri intre reviziile 2 si 3 | Cod sursa (job #2166819)
#include <bits/stdc++.h>
#define LL long long
using namespace std;
ifstream fin("deque.in");
ofstream fout("deque.out");
LL sum;
int n, k, x, index;
deque<pair<int, int> > d; /// pair( element, index )
int main()
{
fin >> n >> k >> x;
d.push_front(make_pair(x, 1));
for(index=2; index<=k; index++)
{
fin >> x;
while(d.back().first >= x)
d.pop_back();
d.push_back(make_pair(x,index));
}
sum = d.front().first;
for(; index<=n; index++)
{
fin >> x;
while(d.back().first >= x)
d.pop_back();
d.push_back(make_pair(x,index));
while(index - d.front().second == k)
d.pop_front();
sum+=d.front().first;
}
fout<<sum;
return 0;
}