Pagini recente » Cod sursa (job #672153) | Istoria paginii runda/dutzpalacsinta | Cod sursa (job #2140384) | Cod sursa (job #2261072) | Cod sursa (job #2047479)
#include <fstream>
#define nmax 5000000
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int top,bottom=1,s,N,K;
struct element{
long long int val;
int poz;
} D[nmax];
void push(long long int x)
{
D[++top].val=x;
}
void pop_top()
{
if(top>0)
--top;
}
void pop_bottom()
{
++bottom;
}
int main()
{
f>>N>>K;
for(int i=1;i<=N;i++)
{
long long int x;
f>>x;
if(i-D[bottom].poz==K)
pop_bottom();
while(x<D[top].val&&top>=bottom)
pop_top();
push(x);
D[top].poz=i;
if(i>=K)
s+=D[bottom].val;
}
g<<s;
return 0;
}