Pagini recente » Cod sursa (job #582864) | Cod sursa (job #476148) | Cod sursa (job #58636) | Cod sursa (job #802345) | Cod sursa (job #1052930)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
long heap[5000005],poz[5000005],a[5000005],nr,k;
long i,x,ki,n;
void urca(long pozi)
{
while(pozi>1&& a[heap[pozi]]<a[heap[pozi/2]])
{
swap(heap[pozi],heap[pozi/2]);
poz[heap[pozi]]=pozi;
poz[heap[pozi/2]]=pozi/2;
pozi/=2;
}
}
void coboara(long pozi)
{
{
long poz1=0;
while(pozi!=poz1) {
poz1=pozi;
if(2*poz1<=n && a[heap[pozi]]>a[heap[2*poz1]])
pozi=2*poz1;
if(2*poz1+1<=n && a[heap[pozi]]>a[heap[2*poz1+1]])
pozi=2*poz1+1;
swap(heap[pozi],heap[poz1]);
poz[heap[pozi]]=pozi;
poz[heap[poz1]]=poz1;
}
}}
int main()
{long ct=1;
long long sum=0;
f>>nr>>ki;
for(i=1;i<=ki;i++)
{
f>>x;
a[++k]=x;
heap[++n]=k;
poz[k]=n;
urca(n);
}
for(i=ki+1;i<=nr;i++)
{
sum+=a[heap[1]];
a[ct]=12000000;
heap[poz[ct]]=heap[n--];
urca(poz[ct]);
coboara(poz[ct]);
f>>x;
a[++k]=x;
heap[++n]=k;
poz[k]=n;
urca(n);
ct++;
}
g<<sum+a[heap[1]];
}