Cod sursa(job #114468)

Utilizator asafteirobertAsaftei Robert asafteirobert Data 14 decembrie 2007 13:19:45
Problema Jocul Flip Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.29 kb
program infoarena_flip;
var i,j,k,k1,k2,m,n,x,y,s,smax,i2,j2,k3:longint;
    a:array[1..16,1..16] of longint;
    b1,b2:array[1..16] of boolean;

begin
assign(input,'flip.in');
reset(input);
read(n);readln(m);
for i:=1 to n do
 for j:=1 to m do
  read(a[i,j]);
close(input);

for i:=1 to n do
 for j:=1 to m do
  begin
   smax:=smax+a[i,j];
  end;


i:=2;
i:=i shl n-2;
i:=i-1;
j:=2;
j:=j shl m-2;
j:=j-1;
for k1:=1 to i do
 for k2:=1 to j do
  begin
   k:=k1;
   x:=1;
   while (k<>0)and(k<>1) do
    begin
     if (k mod 2 = 0) then b1[x]:=false
                      else b1[x]:=true;
     k:=k div 2;
     x:=x+1;
     end;
   if k=0 then b1[x]:=false;
   if k=1 then begin b1[x]:=true; k:=0; end;

   k:=k2;
   y:=1;
   while (k<>0)and(k<>1) do
    begin
     if (k mod 2 = 0) then b2[y]:=false
                      else b2[y]:=true;
     k:=k div 2;
     y:=y+1;
     end;
   if k=0 then b2[y]:=false;
   if k=1 then begin b2[y]:=true; k:=0; end;
s:=0;

for i2:=1 to n do
 for j2:=1 to m do
  begin
   if b1[i2]=true then k3:=-1
                  else k3:=1;
   if b2[j2]=true then k3:=k3*(-1);
   s:=s+a[i2,j2]*k3;
  end;
if s>smax then smax:=s;

  end;

assign(output,'flip.out');
rewrite(output);
writeln(smax);
close(output);

end.