Pagini recente » Cod sursa (job #2895742) | Cod sursa (job #3224382) | Cod sursa (job #2879168) | Cod sursa (job #2214277) | Cod sursa (job #1053951)
#include<cstdio>
#include<algorithm>
#define NM 5000010
using namespace std;
int n,k,x,p,nmax=0;
long long s=0;
pair< int,int > heap[NM],aux;
void urca(int nod)
{
while(nod>1 && heap[nod].first < heap[nod/2].first)
{
swap(heap[nod],heap[nod/2]);
nod/=2;
}
}
void coboara(int nod)
{
while( nod*2+1<=nmax && ( heap[nod].first > heap[nod*2].first || heap[nod].first > heap[nod*2+1].first ) )
if(heap[nod*2+1].first<heap[nod*2].first)
{
swap(heap[nod],heap[nod*2+1]);
nod=nod*2+1;
}
else
{
swap(heap[nod],heap[nod*2]);
nod=nod*2;
}
if(nod*2==nmax && heap[nod].first > heap[nod*2].first)
swap(heap[nod],heap[nod*2]);
}
void adaug(pair<int,int> aux)
{
heap[++nmax]=aux;
urca(nmax);
}
int main()
{
freopen("deque.in","rt",stdin);
freopen("deque.out","wt",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i)
{
scanf("%d",&aux.first);
while(nmax>0 && ( ( heap[1].second <= i-k ) || (heap[1].first >= aux.first) ))
{
heap[1]=heap[nmax--];
coboara(1);
}
aux.second=i;
adaug(aux);
if(i>=k) s+=heap[1].first;
}
printf("%lld\n",s);
return 0;
}