program mehmooni;
const
MAXN = 200000;
type elenco=array [0..MAXN] of longint;
var
N, i, j, h, pp : longint; (*pp=persone presenti*)
A, D : elenco;
strettemano: int64;
uscita:boolean;
Procedure scambia (var x,y: longint);
var t:longint;
begin
t:=x;
x:=y;
y:=t;
end;
Procedure ordinamento (estremoi,estremos: longint; var v : elenco; ordinato:boolean);
var inf, sup, medio:longint;
pivot :longint;
begin
inf:=estremoi;
sup:=estremos;
medio:= (estremoi+estremos) div 2;
pivot:=v[medio];
repeat
if (ordinato) then
begin
while (v[inf]<pivot) do inf:=inf+1;
while (v[sup]>pivot) do sup:=sup-1;
end;
if inf<=sup then
begin
scambia(v[inf],v[sup]);
inf:=inf+1;
sup:=sup-1;
end;
until inf>sup;
if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
end;
begin
{
uncomment the following lines if you want to read/write from files
assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);
}
readln(N);
for i:=0 to N-1 do
readln(A[i], D[i]);
ordinamento (0,N-1,A, true);
ordinamento (0,N-1,D, true);
for i:=0 to N-1 do write(A[i],' '); writeln;
for i:=0 to N-1 do write(D[i],' '); writeln;
strettemano:=0; if a[0]<d[0] then pp:=1
else pp:=0;
j:=0; i:=1; uscita:=false;
while i<N do
begin
write(a[i],' ',d[j],' ');
if (a[i]<d[j]) then begin strettemano:=strettemano + pp; pp:=pp+1; i:=i+1; end
else begin pp:=pp-1; strettemano:=strettemano + pp; j:=j+1;end;
writeln(pp,' ',strettemano);
end;
if i=N then if j<N then for h:=j to N-1 do begin pp:=pp-1; strettemano:=strettemano + pp; end;
strettemano:=strettemano+2*N; (*tutti salutano il padrone di casa quando arrivanoe quando escono*)
writeln(strettemano);
end.
cHJvZ3JhbSBtZWhtb29uaTsKY29uc3QKICAgIE1BWE4gPSAyMDAwMDA7CnR5cGUgZWxlbmNvPWFycmF5IFswLi5NQVhOXSBvZiBsb25naW50Owp2YXI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