Cod sursa(job #309045)

Utilizator flavius_bboian flavius flavius_b Data 29 aprilie 2009 14:55:40
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.59 kb
const d:array[1..2] of integer=(1,-1);   
var f,g:text;   
    sol:array[0..17] of longint;   
    a:array[0..17,0..17] of longint;   
    n,m,i,j,suma:longint;   
procedure calcul;   
 var i,j,s1,s2:longint;   
 begin  
  s1:=0;   
  for j:=1 to m do begin  
   s2:=0;   
   for i:=1 to n do  
    s2:=s2+sol[i]*a[i,j];   
   s1:=s1+abs(s2);   
  end;   
  if suma<s1 then suma:=s1;   
 end;   
procedure back(k:longint);   
 var i:longint;   
 begin  
  if k>n then begin  
   calcul;   
  end  
   else  
    for i:=1 to 2 do begin  
     sol[k]:=d[i];   
     back(k+1);   
    end;   
 end;   
begin  
assign(f,'flip.in');reset(f);   
assign(g,'flip.out');rewrite(g);   
read(f,n,m);   
for i:=1 to n do  
 for j:=1 to m do  
  read(f,a[i,j]);   
suma:=0;   
back(1);   
writeln(g,suma);   
close(f);   
close(g);   
end.  
const d:array[1..2] of integer=(1,-1);
var f,g:text;
    sol:array[0..17] of longint;
    a:array[0..17,0..17] of longint;
    n,m,i,j,suma:longint;
procedure calcul;
 var i,j,s1,s2:longint;
 begin
  s1:=0;
  for j:=1 to m do begin
   s2:=0;
   for i:=1 to n do
    s2:=s2+sol[i]*a[i,j];
   s1:=s1+abs(s2);
  end;
  if suma<s1 then suma:=s1;
 end;
procedure back(k:longint);
 var i:longint;
 begin
  if k>n then begin
   calcul;
  end
   else
    for i:=1 to 2 do begin
     sol[k]:=d[i];
     back(k+1);
    end;
 end;
begin
assign(f,'flip.in');reset(f);
assign(g,'flip.out');rewrite(g);
read(f,n,m);
for i:=1 to n do
 for j:=1 to m do
  read(f,a[i,j]);
suma:=0;
back(1);
writeln(g,suma);
close(f);
close(g);
end.