Cod sursa(job #16897)

Utilizator AymdTrimbitas Viorel Stefan Aymd Data 14 februarie 2007 14:52:21
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.33 kb
var l,c:array[1..16]of longint;
    s,smax,n,m,x,y:longint;
    ll,cc:array[1..17]of 0..1;

procedure cit;
var f:text;
    i,j:longint;
begin
assign(f,'flip.in');
reset(f);
readln(f,n,m);
for i:=1 to n do begin
   for j:=1 to m do begin
      read(f,x);
      l[i]:=l[i]+x;
      c[j]:=c[j]+x;
   end;
   readln(f);
end;
end;

procedure rez;
var i,j:longint;
begin
for i:=1 to n do smax:=smax+l[i];
while cc[m+1]=0 do begin
   i:=1;
   while cc[i]=1 do begin
                    cc[i]:=0;
                    i:=i+1;
                    end;
   cc[i]:=1;
   for j:=1 to n+1 do ll[j]:=0;
   if cc[m+1]=0 then
      while ll[n+1]=0 do begin
         j:=1;
         while ll[j]=1 do begin
                          ll[j]:=0;
                          j:=j+1;
                          end;
         ll[j]:=1;
         s:=0;
         if ll[n+1]=0 then begin
                           for x:=1 to m do if cc[x]=0 then s:=s+c[x]
                                                       else s:=s-c[x];
                           for x:=1 to n do if ll[x]=1 then s:=s-2*l[x];
                           if s>smax then smax:=s;
                           end;
    end;
end;
end;

procedure tip;
var g:text;
begin
assign(g,'flip.out');
rewrite(g);
write(g,smax);
close(g);
end;

begin
cit;
rez;
tip;
end.