Cod sursa(job #1518086)

Utilizator guideamarinGuidea Marin guideamarin Data 5 noiembrie 2015 14:29:33
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.9 kb
program fil;
type p=array[1..16,1..16] of longint;
tabel=array[1..16] of longint;
var i,j,n,m,sp,sm,sum,r,aux,s:longint;y:tabel;x:p; f1,f2:text;
begin
assign(f1,'flip.in');assign(f2,'flip.out'); reset(f1);rewrite(f2);
readln(f1,n,m);
for i:=1 to 16 do y[i]:=0;

for i:=1 to n do
begin
 sm:=0;  sp:=0;
for j:=1 to m do
begin

read(f1,x[i,j]);
if x[i,j]<0 then sm:=sm+x[i,j] else sp:=sp+x[i,j];
end  ;
if abs(sm)>abs(sp) then begin aux:=-sm; sm:=-sp; sp:=aux; y[i]:=1;  end;
s:=s+sp+sm;
readln(f1);
end;
for j:=1 to m do
begin
sm:=0;sp:=0;
for i:=1 to n do
begin
if (y[i]=1) then begin
s:=s+x[i,j];
if -x[i,j]<0 then sm:=sm-x[i,j] else sp:=sp-x[i,j]; end
else
begin
s:=s-x[i,j];
if x[i,j]<0 then   sm:=sm+x[i,j] else sp:=sp+x[i,j]; end  ;
end;
if abs(sm)>abs(sp) then begin
aux:=abs(sm);
sm:=-sp;
sp:=aux;  end;
s:=s+sp+sm;
end;
writeln(f2,s);
close(f1);close(f2);
end.