Cod sursa(job #14915)

Utilizator charmedRoatis Carmen Maria charmed Data 10 februarie 2007 10:41:21
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.21 kb
program flip;
var m,n,i,j:byte;
    s,ss,smax:longint;
    a:array[1..16,1..16] of longint;
    x:array[1..32]of integer;
    f,h:text;
    g:boolean;
begin
assign(f,'flip.in');
assign(h,'flip.out');
reset(f);rewrite(h);
readln(f,n,m);
for i:=1 to n do
 for j:=1 to m do read(f,a[i,j]);
ss:=0;
repeat
for i:=1 to n do
 begin
 s:=0;
 for j:=1 to m do s:=s+a[i,j];
 if s<0 then x[i]:=1;
 end;
for i:=1 to m do
 begin
 s:=0;
 for j:=1 to n do
  if x[j]=0 then s:=s+a[i,j]
            else s:=s-a[i,j];
 if s<0 then x[n+i]:=1;
 end;
g:=true;
for i:=1 to n do
 begin
 s:=0;
 for j:=1 to m do
  if  ((x[i]=1)and(x[n+j]=1))or
     ((x[i]=0)and(x[n+j]=0)) then s:=s+a[i,j]
     else s:=s-a[i,j];
 if s<0 then g:=false;
 if not g then break;
 end;
 if g then
 for i:=1 to m do
 begin
 s:=0;
 for j:=1 to n do
  if  ((x[m+j]=1)and(x[i]=1))or
     ((x[j+m]=0)and(x[i]=0)) then s:=s+a[i,j]
     else s:=s-a[i,j];
 if s<0 then g:=false;
 if not g then break;
 end;

until g;
smax:=0;
for i:=1 to n do
 for j:=1 to m do
  if ((x[i]=1)and(x[n+j]=1))or
     ((x[i]=0)and(x[n+j]=0))
            then smax:=smax+a[i,j]
            else smax:=smax-a[i,j];
writeln(h,smax);
close(f);close(h);
end.