Cod sursa(job #34721)

Utilizator andradaqAndrada Georgescu andradaq Data 21 martie 2007 11:41:23
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.9 kb
var f:text;
    a:array[1..16,1..16] of int64;
    linia,coloana,laux,caux:array[1..16] of int64;
    x,n,m,i,j,k:longint;
    sol,suma,aux:int64;

begin
assign(F,'flip.in'); reset(F);
readln(f,n,m);
fillchar(linia,sizeof(linia),0);
fillchar(coloana,sizeof(coloana),0);
suma:=0;
for i:=1 to n do
 begin
 for j:=1 to m do
  begin
  read(f,a[i,j]);
  laux[i]:=laux[i]+a[i,j];
  caux[j]:=caux[j]+a[i,j];
  aux:=aux+a[i,j];
  end;
 readln(F);
 end;
close(F);
x:=0;
sol:=aux;
for i:=1 to (1 shl n)+1 do
 begin
 suma:=aux;
 linia:=laux;
 coloana:=caux;
 for j:=0 to n-1 do
  if (1 shl j)and x>0 then
    begin
    suma:=suma-2*linia[j+1];
    for k:=1 to m do
      coloana[k]:=coloana[k]-2*a[j+1,k];
    end;
 for j:=1 to m do
  if coloana[j]<0 then suma:=suma-2*coloana[j];
 if suma >sol then sol:=suma;
 inc(x);
 end;
assign(f,'flip.out');rewrite(F);
writeln(f,sol);
close(f);
end.