Pagini recente » Cod sursa (job #2140801) | Cod sursa (job #3175256) | Cod sursa (job #2949578) | Cod sursa (job #27421) | Cod sursa (job #2264364)
#include <fstream>
#include <deque>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
deque<int>d;
int n,i,k,v[5000005];
long long s;
int main()
{
f>>n>>k;
for(i=1;i<=n;i++)f>>v[i];
for(i=1;i<=n;i++)
{
while(!d.empty()&&v[d.back()]>=v[i])
{
d.pop_back();
}
d.push_back(i);
if(i-d.front()==k)d.pop_front();
if(i>=k)s=s+v[d.front()];
}
g<<s;
return 0;
}