Cod sursa(job #188556)

Utilizator FllorynMitu Florin Danut Flloryn Data 8 mai 2008 22:09:15
Problema Jocul Flip Scor 30
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.65 kb
program pascal;
var f,g:text; u,v:array[1..100] of longint; ok:boolean;
    max,i,j,p,sum,s,n,m:longint;
    a:array[1..100,1..100] of longint;
 procedure citire;
 begin
   s:=0;
  for i:=1 to n do
   begin
     for j:=1 to m do
           begin
             read(f,a[i,j]);
             v[i]:=v[i]+a[i,j];
             u[j]:=u[j]+a[i,j];
           end;
     readln(f);
     s:=s+v[i];
   end;
 end;

begin
 assign(f,'flip.in'); reset(f);
 assign(g,'flip.out'); rewrite(g);
 readln(f,n,m);
 max:=-maxint;
 citire;

repeat
 ok:=true;
 if s>max then max:=s;
  for i:=1 to n do
   begin
    if v[i]<0 then begin
                    sum:=0;
                    for j:=1 to m do
                     begin
                      a[i,j]:=-a[i,j];
                      sum:=sum+a[i,j];
                      u[j]:=u[j]+2*a[i,j];
                     end;
                     s:=s-v[i];
                     v[i]:=sum;
                     s:=s+v[i];
                     if s>max then max:=s;
                     ok:=false;
                   end;
   end;
  for j:=1 to m do
   begin
    if u[j]<0 then begin
                     sum:=0;
                      for i:=1 to n do
                       begin
                        a[i,j]:=-a[i,j];
                        sum:=sum+a[i,j];
                        v[i]:=v[i]+2*a[i,j];
                       end;
                       s:=s-u[j];
                       u[j]:=sum;
                       s:=s+u[j];
                       if s>max then max:=s;
                       ok:=false;
                    end;
   end;
 until ok;
 write(g,max);
 close(f);
 close(g);
end.