Pagini recente » Cod sursa (job #336381) | Cod sursa (job #1781306) | Cod sursa (job #2446109) | Cod sursa (job #883802) | Cod sursa (job #1201583)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("interclasari.in");
ofstream cout("interclasari.out");
long H[4000013],i,j,n,nod,l(0),aux,sol(0);
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;
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)
{
H[++l]=nod;
heap_up(l);
}
void del(long &l)
{
cout<<H[1]<<" ";
H[1]=H[l];
--l;
heap_down(1);
}
int main()
{
cin>>n;
for (i=1;i<=n;++i) {
cin>>aux;
sol+=aux;
for (j=1;j<=aux;j++) {
cin>>nod;
add(nod);
}
}
cout<<sol<<"\n";
while (l) del(l);
return 0;
}