Pagini recente » Cod sursa (job #1253431) | Cod sursa (job #984015) | Cod sursa (job #1785165) | Cod sursa (job #1381637) | Cod sursa (job #772717)
Cod sursa(job #772717)
Uses math;
{$inline on}
Const
finp='deque.in';
fout='deque.out';
Type
mat=array [1..5000000] of longint;
Var a,stack:mat;
n,k,f,r:longint;
time:longint;
res:int64;
fi,fo:text;
Procedure Doc; inline;
Var i:longint;
Begin
Assign(fi,finp);
Reset(fi);
Read(fi,n,k);
For i:=1 to n do Read(fi,a[i]);
Close(fi);
end;
Procedure Push(x:longint);
Begin
inc(r);
stack[r]:=x;
end;
Procedure Lam;
Var i:longint;
Begin
f:=1;
r:=0;
// Push(1);
For i:=1 to n do
Begin
While (f<=r) and (a[i]<=a[stack[r]]) do dec(r);
inc(r);
stack[r]:=i;
If stack[f]=i-k then inc(f);
If i>=k then res:=res+a[stack[f]];
end;
end;
Procedure Inkq; inline;
Begin
Assign(fo,fout);
Rewrite(fo);
Write(fo,res);
Close(fo);
end;
Begin
// time:=gettickcount;
Doc;
Lam;
Inkq;
// Write((gettickcount-time)/1000);
end.