program town; function max(p,q:longint):longint; {This function computes the maximumum of two given longints} begin if p>=q then max:=p else max:=q end; const MAXHEIGHT=5000; {The maximumum height of buildings is 5000 cubes} var heights_front:array[0..MAXHEIGHT] of longint; heights_side:array[0..MAXHEIGHT] of longint; width,length:longint; higher_front,higher_side:longint; minimum,maximum:longint; possible:boolean; a,b:longint; f:text; begin {Opening and reading the input file} assign(f,'TOWN.IN'); reset(f); readln(f,width,length); for a:=0 to MAXHEIGHT do begin heights_front[a]:=0; heights_side[a]:=0; end; {Sorting heights of buildings using countsort while reading them} for a:=1 to width do begin readln(f,b); inc(heights_front[b]); end; for a:=1 to length do begin readln(f,b); inc(heights_side[b]); end; close(f); a:=MAXHEIGHT; {= the building height which we scan} possible:=true; {Can be the town built?} minimum:=0; maximum:=0; {How many cubes can Jack use?} higher_front:=0; higher_side:=0; {How many buildings are higher then a?} while (a>=0) and possible do {Scanning all possible heights of building} begin {Checking the possibility of building the town} possible:=not((higher_front=0) and (higher_side=0) and ( ((heights_front[a]<>0) and (heights_side[a]=0)) or ((heights_front[a]=0) and (heights_side[a]<>0)) )); {Updating all variables} minimum:=minimum+max(heights_front[a],heights_side[a])*a; maximum:=maximum+higher_front*higher_side; higher_front:=higher_front+heights_front[a]; higher_side:=higher_side+heights_side[a]; dec(a); end; {Writing results to the output file} assign(f,'TOWN.OUT'); rewrite(f); if possible then writeln(f,minimum,' ',maximum) else writeln(f,'No solution.'); close(f); end.