Pagini recente » Cod sursa (job #3239244) | Cod sursa (job #1894643) | Cod sursa (job #1610041) | Cod sursa (job #1321554) | Cod sursa (job #1052953)
#include <iostream>
#include<fstream>
#include<algorithm>
using namespace std;
ifstream f("deque.in");
ofstream g("deque.out");
int heap[5000005],poz[5000005],a[5000005],nr,k;
int i,x,ki,n;
void urca(int pozi)
{
while(pozi>1&& a[heap[pozi]]<a[heap[pozi>>1]])
{
swap(heap[pozi],heap[pozi>>1]);
poz[heap[pozi]]=pozi;
poz[heap[pozi>>1]]=pozi>>1;
pozi>>=1;
}
}
void coboara(int pozi)
{
{
int poz1=0;
while(pozi!=poz1) {
poz1=pozi;
if((poz1<<1)<=n && a[heap[pozi]]>a[heap[poz1<<1]])
pozi=poz1<<1;
if((poz1<<1)+1<=n && 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()
{int 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]];
}