Cod sursa(job #1527254)

Utilizator gra99Graur Gabriel gra99 Data 17 noiembrie 2015 22:42:03
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.41 kb
program flip;
   var f,g:textfile;
   a:array[1..20,1..20] of longint;
   m,n,i,j:byte;
   s,k,sum,k1:longint;
 begin
 assign(f,'flip.in');reset(f);
 assign(g,'flip.out');rewrite(g);
 read(f,m,n);
 if (m<=16) and (n<=16) then begin
 s:=0;k:=0;sum:=0;k1:=0;
 for i:=1 to n do
     for j:=1 to m do read(f,a[i,j]);
     if (a[i,j]>=-1000000) and (a[i,j]<=1000000) then
  begin
  repeat
  inc(k1);
  for i:=1 to n do begin
     for j:=1 to m do
                      begin
                      if a[i,j]>=0 then s:=s+a[i,j] else
                                                     if a[i,j]<0 then k:=k+a[i,j];
                      end;
                      if abs(s)<abs(k) then for j:=1 to m do a[i,j]:=-1*a[i,j];
                      s:=0;k:=0;
                   end;
 for j:=1 to m do begin
               for i:=1 to n do
                                begin
                                 if a[i,j]>=0 then s:=s+a[i,j] else
                                                     if a[i,j]<0 then k:=k+a[i,j];
                                 end;
                                 if abs(s)<abs(k) then for i:=1 to n do a[i,j]:=-1*a[i,j];
                                s:=0;k:=0;
                   end;

                   until k1=10000;
 for i:=1 to n do
            for j:=1 to m do
                      sum:=sum+a[i,j];
 writeln(g,sum);
 end;
 end;
 close(f);
 close(g);
 end.