Cod sursa(job #140374)

Utilizator andumMorie Daniel Alexandru andum Data 21 februarie 2008 20:22:08
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.86 kb
var a:array[1..17,1..17] of longint;
    n,m,i,j,min,s,s1,s2:longint;
    f,g:text;
    ok:boolean;
begin
assign(f,'flip.in');
assign(g,'flip.out');
reset(f);
rewrite(g);
readln(f,m,n);
ok:=false;
for i:=1 to m do begin
for j:=1 to n do begin
                 read(f,a[i,j]);
                 s:=s+a[i,j];
                 if a[i,j]<0 then ok:=true;
                 end;
                 readln(f);
                 end;
if not ok then writeln(g,s)
          else
begin
min:=maxint;
for i:=1 to m do begin
                 s1:=0;
for j:=1 to n do s1:=s1+a[i,j];
                 if s1<min then min:=s1;
                 end;
s:=s-(2*min);
for i:=1 to n do begin
                 s1:=0;
for j:=m downto 1 do s1:=s1+a[j,i];
                 if s1<min then min:=s1;
                 end;
s:=s-(2*min);
writeln(g,s);
end;
close(f);
close(g);
end.