Cod sursa(job #12507)

Utilizator floringh06Florin Ghesu floringh06 Data 4 februarie 2007 11:31:59
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.46 kb
type smen=record neg,poz:integer; end;
var fi,fo:text;
    n,m,i,j,slin,scol,max1,max2:longint;

    a,aux:array[1..100,1..100] of longint;
    lin,col:array[1..100] of smen;




  procedure invcol(x:integer);
   var i:integer;
    begin
      for i:=1 to n do
        aux[i,x]:=-aux[i,x];
    end;

  procedure invlin(x:integer);
   var j:integer;
    begin
      for j:=1 to m do
        aux[x,j]:=-aux[x,j];
    end;

  function sumcol2(x:integer):longint;
   var sum,j:longint;
    begin
     sum:=0;
      for i:=1 to n do
       if a[i,x]<0 then sum:=sum+a[i,x];
     sumcol2:=sum;
    end;


  function sumcol1(x:integer):longint;
   var sum,j:longint;
    begin
     sum:=0;
      for i:=1 to n do
        if a[i,x]>=0 then sum:=sum+a[i,x];
     sumcol1:=sum;
    end;



  function sumlin2(x:integer):longint;
   var sum,j:longint;
    begin
     sum:=0;
      for j:=1 to m do
       if a[x,j]<0 then sum:=sum+a[x,j];
     sumlin2:=sum;
    end;


  function sumlin1(x:integer):longint;
   var sum,j:longint;
    begin
     sum:=0;
      for j:=1 to m do
        if a[x,j]>=0 then sum:=sum+a[x,j];
     sumlin1:=sum;
    end;

 function suma:longint;
   var i,j:integer;
       sum:longint;
     begin
     sum:=0;
      for i:=1 to n do
       for j:=1 to m do
         sum:=sum+aux[i,j];
      suma:=sum;
     end;


  procedure gosolve;
    begin
     for i:=1 to n do
       begin
         lin[i].poz:=sumlin1(i);
         lin[i].neg:=sumlin2(i);
       end;
     for j:=1 to m do
       begin
         col[j].poz:=sumcol1(j);
         col[j].neg:=sumcol2(j);
       end;
     for i:=1 to n do
         if lin[i].poz<abs(lin[i].neg) then invlin(i);
     for i:=1 to m do
         if col[i].poz<abs(col[i].neg) then invcol(i);
     max1:=suma;
     aux:=a;
     for i:=1 to m do
         if col[i].poz<abs(col[i].neg) then invcol(i);
     for i:=1 to n do
         if lin[i].poz<abs(lin[i].neg) then invlin(i);
     max2:=suma;
     if max1>max2 then writeln(fo,max1)
        else writeln(fo,max2);
  end;

begin
 assign(fi,'flip.in'); reset(fi);
 assign(fo,'flip.out'); rewrite(fo);
 readln(fi,n,m);
 for i:=1 to n do
  for j:=1 to m do
   begin
     read(fi,a[i,j]);
     aux[i,j]:=a[i,j];
   end;
   gosolve;
{  for i:=1 to n do
    write(fo,lin[i].poz,'/',lin[i].neg,' ');
  writeln(fo);
  for i:=1 to m do
    write(fo,col[i].poz,'/',col[i].neg,' ');
}
close(fi);
close(fo);
end.