# Cod sursa(job #275022)

Utilizator Data 10 martie 2009 10:15:23 Jocul Flip 30 fpc done Arhiva de probleme 1.63 kb
``````var f,g:text;
max,xx,ii,jj,neg,n,m,i,j,sn,sp,s:longint;
a:array[1..100,1..100] of longint;
vl,vc:array[1..100] of longint;
begin
assign(f,'flip.in');reset(f);
assign(g,'flip.out');rewrite(g);
for i:=1 to n do
for j:=1 to m do
for i:=1 to n do begin
sp:=0;
sn:=0;
for j:=1 to m do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vl[i]:=sp+sn;
end;
for j:=1 to m do begin
sp:=0;
sn:=0;
for i:=1 to n do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vc[j]:=sp+sn;
end;
repeat
max:=0;
xx:=0;
for i:=1 to n do
if vl[i]<max then begin
max:=vl[i];
xx:=1;
ii:=i;
end;
for j:=1 to m do
if vc[j]<max then begin
max:=vc[j];
xx:=2;
jj:=j;
end;
if xx=1 then begin
for j:=1 to m do a[ii,j]:=-a[ii,j];
for j:=1 to m do begin
sp:=0;
sn:=0;
for i:=1 to n do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vc[j]:=sp+sn;
end;
for i:=1 to n do begin
sp:=0;
sn:=0;
for j:=1 to m do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vl[i]:=sp+sn;
end;
end;
if xx=2 then begin
for i:=1 to n do a[i,jj]:=-a[i,jj];
for i:=1 to n do begin
sp:=0;
sn:=0;
for j:=1 to m do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vl[i]:=sp+sn;
end;
for j:=1 to m do begin
sp:=0;
sn:=0;
for i:=1 to n do
if a[i,j]<0 then sn:=sn+a[i,j]
else sp:=sp+a[i,j];
vc[j]:=sp+sn;
end;
end;
neg:=0;
for i:=1 to n do if vl[i]<0 then inc(neg);
for j:=1 to m do if vc[i]<0 then inc(neg);
until neg=0;
s:=0;
for i:=1 to n do
for j:=1 to m do
s:=s+a[i,j];
write(g,s);
close(f);
close(g);
end.
``````