Cod sursa(job #68485)

Utilizator mihaelams1Mihaela Rusu mihaelams1 Data 28 iunie 2007 10:09:49
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.81 kb
var f,g:text;
a:array[1..16,1..16] of longint;
minte,n,m,i,j,k:integer;
sum,neg,poz,max:longint;
b:array[0..16] of integer;

procedure adun;
begin
k:=m;
repeat
if b[k]=0 then begin b[k]:=1; minte:=0; end
else begin minte:=1; b[k]:=0; end;
dec(k);
until minte=0;
end;

procedure calc;
begin
sum:=0;
for i:=1 to n do begin
neg:=0;
poz:=0;
for j:=1 to m do
if b[j]=1 then if -a[i,j]<0 then neg:=neg-a[i,j]
else poz:=poz-a[i,j]
else if a[i,j]<0 then neg:=neg+a[i,j]
else poz:=poz+a[i,j];
if neg+poz<0 then sum:=sum-poz-neg
else sum:=sum+poz+neg;
end;
if sum>max then max:=sum;
end;

begin
assign(f,'flip.in');
reset(f);
read(f,n,m);
for i:=1 to n do
for j:=1 to m do
read(f,a[i,j]);
close(f);
repeat
calc;
adun;
until b[0]=1;
assign(f,'flip.out');
rewrite(f);
write(f,max);
close(f);
end.