Cod sursa(job #268472)

Utilizator andreirulzzzUPB-Hulea-Ionescu-Roman andreirulzzz Data 1 martie 2009 12:04:40
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.18 kb
var a:array[0..20,0..16] of longint;
    f:text;
    i,smax,j,s,m,n:integer;
{-------------------------}
procedure suma;
begin
     assign(f,'flip.in');
     reset(f);
     readln(f,n,m);
     for i:=1 to n do
         for j:=1 to m do begin
             read(f,a[i,j]);
             a[0,j]:=a[0,j]+a[i,j];
             a[i,0]:=a[i,0]+a[i,j];
             smax:=smax+a[i,j];
             end;
close(f);
end;
{-------------------------}
procedure inlocuirelin;
begin
     for j:=1 to m do begin
         a[i,j]:=a[i,j]*(-1);
         a[i,0]:=a[i,0]+2*a[i,j];
         s:=s+2*a[i,j];
         a[0,j]:=a[0,j]+2*a[i,j];
     end;
end;
{-------------------------}
procedure inlocuirecol;
begin
     for j:=1 to n do begin
         a[j,i]:=a[j,i]*(-1);
         a[j,0]:=a[j,0]+2*a[j,i];
         s:=s+2*a[j,i];
         a[0,i]:=a[0,i]+2*a[j,i];
         end;
end;
{-------------------------}
begin
suma;
s:=smax;
repeat
      for i:=1 to n do
          if a[i,0]<0 then inlocuirelin;
      for i:=1 to m do
          if a[0,i]<0 then inlocuirecol;
          if s>smax then s:=smax;
until s<=smax;
assign(f,'flip.out');
rewrite(f);
write(f,smax);
close(f);
end.