Pagini recente » Cod sursa (job #2431846) | Cod sursa (job #861888) | Cod sursa (job #1543255) | Cod sursa (job #157961) | Cod sursa (job #1273939)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("interclasari.in");
ofstream cout("interclasari.out");
long H[4000013],i,j,n,nod,l,aux,sol(0),k;
void heap_up(long nod)
{
if (H[nod]<H[nod/2] && nod>1)
{
swap(H[nod],H[nod/2]);
heap_up(nod/2);
}
}
void heap_down(long nod)
{
long fiu(9999);
while (fiu)
{
fiu=0;
if (nod*2<=l)
{
fiu=nod*2;
if (nod*2+1<=l && H[nod*2+1]<H[fiu]) fiu=nod*2+1;
}
if (H[fiu]>=H[nod]) fiu=0;
if (fiu){
swap(H[fiu],H[nod]);
nod=fiu;
}
}
}
void add(long nod,long &l)
{
H[++l]=nod;
heap_up(l);
}
void dell(long &l)
{
cout<<H[1]<<" ";
swap(H[1],H[l--]);
heap_down(1);
}
int main()
{
cin>>k;
while (k--)
{
cin>>n;
sol+=n;
for (j=1;j<=n;++j)
{
cin>>nod;
add(nod,l);
}
}
cout<<sol<<"\n";
while (l) dell(l);
return 0;
}