Cod sursa(job #292260)

Utilizator katamashCatalin Tamas katamash Data 30 martie 2009 22:06:39
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.51 kb
var n,m : byte;      
    a : array[1..17,1..17] of longint;      
    b : array[1..17] of boolean;      
    max : longint;      
     
procedure Citeste;      
var Intrare : text;      
    i,j : byte;      
begin     
  assign(Intrare,'flip.in');      
  reset(Intrare);      
  readln(Intrare,n,m);      
  for i:=1 to n do     
   for j:=1 to m do read(Intrare,a[i,j]);      
  close(Intrare);      
end;      
     
procedure Urmatorul;      
var i : byte;      
begin     
  i:=1;      
  while b[i] do begin     
    b[i]:=false;      
    i:=i+1;      
  end;      
  b[i]:=true;      
end;      
     
procedure VerificaSuma;      
var i,j : byte;      
    s,t : longint;      
begin     
  t:=0;      
  for i:=1 to n do begin     
    s:=0;      
    for j:=1 to m do     
     if b[j] then s:=s+a[i,j]      
     else s:=s-a[i,j];      
    if s<0 then t:=t-s      
    else t:=t+s;      
  end;      
  if t>max then max:=t;      
end;      
     
procedure Calculeaza;      
var x,i : longint;      
begin     
  x:=1;      
  for i:=1 to 17 do b[i]:=false;      
  for i:=1 to m do x:=x*2;      
  for i:=1 to x do begin     
    VerificaSuma;      
    Urmatorul;      
  end;      
end;      
     
procedure Scrie;      
var Iesire : text;      
begin     
  assign(Iesire,'flip.out');      
  rewrite(Iesire);      
  write(Iesire,max);      
  close(Iesire);      
end;      
     
begin     
  Citeste;      
  Calculeaza;      
  Scrie;      
end.