Cod sursa(job #172165)

Utilizator kolapsysPostelnicu Dan Marian kolapsys Data 5 aprilie 2008 21:14:55
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.07 kb
{ http://infoarena.ro/problema/flip }
type matrice = array[1..16,1..16] of longint;
     stiva = array[1..16] of shortint;
var
   f, g : text;
   a : matrice;
   st : stiva;
   i, j, n, m : byte;
   smax : longint;
procedure suma;
var i, j : byte;
    s, scol : longint;
begin
    s := 0;
    for i := 1 to n do
        begin
        scol := 0;
        for j := 1 to m do
                scol := scol + st[j] * a[i,j];
        if scol < 0 then s := s - scol
                    else s := s + scol;
        end;
    if s > smax then smax := s;
end;

procedure back(i : integer);
begin
    if i = m then suma
             else begin
                  st[i] := 1;
                  back(i + 1);
                  st[i] := -1;
                  back(i + 1);
                  end;
end;

BEGIN
   assign(f, 'flip.in'); reset(f);
   assign(g, 'flip.out'); rewrite(g);
   readln(f, n, m);
   for i := 1 to n do
        for j := 1 to m do
                read(f, a[i,j]);
   smax := -maxlongint;
   back(1);
   writeln(g, smax);
   close(f); close(g);
END.