Cod sursa(job #915454)

Utilizator DjokValeriu Motroi Djok Data 15 martie 2013 01:00:55
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.62 kb

  var a:array[0..200,0..200] of longint;
      bit:array[0..20] of integer;
      fi, fo:text;
      i, j, m, n:integer;
      aux, poz, s, s1, k, smax, s2:longint;
begin
 assign(fi, 'flip.in');
 reset(fi);
 assign(fo, 'flip.out');
 rewrite(fo);
 read(fi, n,m);
  for j:=1 to n do
   for i:=1 to m do
    read(fi, a[i,j]);
    smax:=0;
     for i:=1 to (1 shl n)-1 do begin
            aux:=i; poz:=0;
            while aux>0 do begin
               if aux mod 2=0 then bit[poz]:=1
                              else bit[poz]:=0;
                aux:=aux shr 1;
                poz:=poz+1;
                end;
                s:=0;
     for k:=1 to m do begin
      s1:=0;
       for j:=1 to n do
        if bit[j]=1 then s1:=s1+a[k,j]
                    else s1:=s1-a[k,j];
        if (s1<0) then s:=s-s1
                  else s:=s+s1;
                  end;
                  if s>smax then smax:=s;
                  end;
                  
       for i:=1 to (1 shl m)-1 do begin
            aux:=i; poz:=0;
            while aux>0 do begin
               if aux mod 2=0 then bit[poz]:=1
                              else bit[poz]:=0;
                aux:=aux shr 1;
                poz:=poz+1;
                end;
    for k:=1 to n do begin
      s1:=0;
       for j:=1 to m do
        if bit[j]=1 then s1:=s1+a[j,k]
                    else s1:=s1-a[j,k];
        if (s1<0) then s2:=x-s1
                  else s2:=2+s1;
                  end;
       if s2>s then s:=s2;
                  end;
                 if s>smax then smax:=s;
            writeln(fo,smax);
 close(fi);
 close(fo);
end.