[an error occurred while processing this directive]
Listing ORAS.PAS 
 Program town; 
 const ni='town.in'; 
 no='town.out'; 
 hmax=5000; 
 var f:Text; 
 a,b,c:array [0..hmax] of Longint; 
 n,m,i,j,k,maxa,maxb,min,max:Longint; 
 
  procedure load; 
 begin 
 Assign(f,ni); Reset(f); 
 Readln(f,n,m); 
 for i:=0 to hmax 
do 
 begin a[i]:=0; b[i]:=0 end; 
 maxa:=0; 
 for i:=1 to n 
do 
 begin
 Readln(f,k); 
 a[k]:=a[k]+1; 
 if k>maxa then maxa:=k 
 end; 
 maxb:=0; 
 for i:=1 to m 
do 
 begin
 Readln(f,k); b[k]:=b[k]+1; 
 if k>maxb then maxb:=k 
 end; 
 Close(f) 
 end; 
 
 procedure imposibil; 
 begin 
 if maxa<>maxb 
then 
 begin
 Assign(f,no); Rewrite(f); 
 Writeln(f,'No solution.'); 
 Close(f); Halt 
 end 
 end; 
 
 procedure minim; 
 begin 
 min:=0; 
 for i:=0 to hmax do min:=min+i*a[i]; 
 for i:=0 to hmax do 
 if b[i]>a[i] 
 then min:=min+i*(b[i]-a[i]) 
 end; 
 
 procedure maxim; 
 begin 
 max:=0; 
 for i:=0 to hmax do 
 max:=max+i*a[i]*m; 
 c[hmax]:=0; j:=0; 
 for i:=hmax-1 downto 0 
do 
 begin
 j:=j+a[i+1]; c[i]:=c[i+1]+j 
 end; 
 for i:=0 to hmax do 
 max:=max-c[i]*b[i] 
 end; 
 
 procedure scrie; 
 begin 
 Assign(f,no); 
 Rewrite(f); 
 Writeln(f,min,' ',max); 
 Close(f) 
 end; 
 
 Begin  
 load; imposibil; 
 minim; maxim; 
 scrie 
 End.

[cuprins]