Pagini recente » Cod sursa (job #2302375) | Cod sursa (job #2147301) | Cod sursa (job #1957204) | Cod sursa (job #210104) | Cod sursa (job #1450900)
#include <bits/stdc++.h>
using namespace std;
class double_ended_queue
{
private :
struct celula
{
int val;
int ind;
celula *next;
celula *prev;
};
typedef celula *adresa;
adresa top=NULL, bottom=NULL,x,cell;
public :
void pushback(int value, int indice)
{
cell=new celula;
cell->val=value;
cell->ind=indice;
cell->next=NULL;
if (bottom==NULL)
{
cell->prev=NULL;
top=cell;
bottom=cell;
}
else
{
cell->prev=bottom;
bottom->next=cell;
bottom=cell;
}
}
bool empty()
{
if (top==NULL || bottom==NULL) return 1;
return 0;
}
void popfront()
{
if (top!=NULL)
{
x=top;
top->next->prev=NULL;
top=top->next;
delete(x);
}
}
void popback()
{
if (bottom && top)
{
x=bottom;
if (bottom->prev!=NULL) bottom->prev->next=NULL;
bottom=bottom->prev;
delete(x);
}
}
adresa front(){ if (top!=NULL) return top; return NULL; }
adresa back (){ if (bottom!=NULL) return bottom; return NULL; }
};
double_ended_queue Deq;
int n,k,aux,pivot(0),i;
long long sum(0);
int main(void)
{
ifstream cin("deque.in");
ofstream cout("deque.out");
cin>>n>>k;
for (i=1;i<=n;++i)
{
cin>>aux;
while(!Deq.empty() && Deq.back()->val >= aux) Deq.popback();
Deq.pushback(aux,i);
if (i>=k)
{
++pivot;
sum+=Deq.front()->val;
while(Deq.front()->ind <= pivot) Deq.popfront();
}
}
cout<<1LL*sum<<"\n";
return 0;
}