Pagini recente » Cod sursa (job #1732699) | Cod sursa (job #1342778) | Cod sursa (job #2381239) | Cod sursa (job #23137) | Cod sursa (job #1125449)
#include <stdio.h>
#include<iostream>
#include<fstream>
#define maxn 5000010
using namespace std;
int n,k;
int a[maxn], Deque[maxn];
int Front, Back;
long long Sum;
int main()
{
ifstream fin("deque.in");
ofstream fout("deque.out");
int i;
fin>>n>>k;
Front=1;Back=0;
for (i = 1; i <= n; i++)
fin>>a[i];
for(i=1;i<=n;i++){
while(Front<=Back&&a[i]<=a[Deque[Back]])Back--;
Deque[++Back]=i;
if(Deque[Front]==i-k)Front++;
if(i>=k) Sum+=a[Deque[Front]];
}
fout<<Sum;
return 0;
}