Pagini recente » Cod sursa (job #1601289) | Cod sursa (job #247151) | Cod sursa (job #1873385) | Cod sursa (job #2411613) | Cod sursa (job #1047158)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
long heap[5000005],poz[5000005],a[5000005],nr,k;
long i,x;
long long sum;
void urca(long &n,long pozi)
{
while(pozi>1&& a[heap[pozi]]<a[heap[pozi/2]])
{
swap(heap[pozi],heap[pozi>>1]);
poz[heap[pozi]]=pozi;
poz[heap[pozi>>1]]=pozi>>1;
pozi=pozi>>1;
}
}
void coboara(long &n,long pozi)
{
{
long poz1=0;
while(pozi!=poz1) {
poz1=pozi;
if(poz1<<1<=k && a[heap[pozi]]>a[heap[pozi<<1]])
pozi=poz1<<2;
if((poz1<<1)+1<=k && a[heap[pozi]]>a[heap[(poz1<<1)+1]])
pozi=(poz1<<1)+1;
swap(heap[pozi],heap[poz1]);
poz[heap[pozi]]=pozi;
poz[heap[poz1]]=poz1;
}
}}
int main()
{long n=0,ct=1,lung;
freopen("deque.in","r",stdin);
freopen("deque.out","w",stdout);
scanf("%ld%ld",&nr,&lung);
for(i=1;i<=lung;i++)
{ scanf("%ld",&x);
a[++n]=x;
heap[++k]=n;
poz[n]=k;
urca(n,k);
}
for(i=lung+1;i<=nr;i++)
{sum+=a[heap[1]];
a[ct]=-10000001;
urca(n,poz[ct]);
poz[heap[1]]=0;
heap[1]=heap[k--];
poz[heap[1]]=1;
coboara(n,1);
scanf("%ld",&x);
a[++n]=x;
heap[++k]=n;
poz[n]=k;
urca(n,k);
ct++;
}
sum+=a[heap[1]];
printf("%lld",sum);
}