Pagini recente » Cod sursa (job #638941) | Cod sursa (job #2206824) | Cod sursa (job #1543310) | Cod sursa (job #1044509) | Cod sursa (job #1801795)
#include <cstdio>
#include <deque>
#define Nmax 5000010
using namespace std;
deque < int > deq;
long long suma;
int vec[Nmax];
int n,k;
void read()
{
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%d %d",&n,&k);
for(int i = 1 ; i < k ; i++)
{
scanf("%d ",&vec[i]);
while(!deq.empty() && vec[deq.back()] >= vec[i])
deq.pop_back();
deq.push_back(i);
}
for(int i = k ; i <= n ; i++)
{
scanf("%d ",&vec[i]);
if(!deq.empty() && deq.front() <= i - k )
deq.pop_front();
while(!deq.empty() && vec[deq.back()] >= vec[i])
deq.pop_back();
deq.push_back(i);
suma += vec[deq.front()];
}
}
int main()
{
read();
printf("%lld",suma);
return 0;
}