Pagini recente » Cod sursa (job #1258463) | Cod sursa (job #1001406) | Cod sursa (job #980139) | Cod sursa (job #321657) | Cod sursa (job #601049)
Cod sursa(job #601049)
type matrice=array[1..16,1..16] of longint;
var a:matrice; x:char; y,max,N,M:int64; F,G:text; i,j:byte;
procedure CautaCandidat;
var max,neg,pos:int64; i,j:byte;
begin
max:=0;
for j:=1 to M do begin
neg:=0; pos:=0;
for i:=1 to N do
if a[i,j]<0 then neg:=neg+a[i,j]
else pos:=pos+a[i,j];
if abs(neg)-abs(pos)>max then
begin
x:='c';
y:=j;
max:=abs(neg)-abs(pos);
end;
end;
for i:=1 to N do begin
neg:=0; pos:=0;
for j:=1 to M do
if a[i,j]<0 then neg:=neg+a[i,j]
else pos:=pos+a[i,j];
if abs(neg)-abs(pos)>max then
begin
x:='l';
y:=i;
max:=abs(neg)-abs(pos);
end;
end;
end;
procedure Flip(x:char; y:integer);
var i,j:byte;
begin
if x='l' then
for j:=1 to M do
a[y,j]:=a[y,j]*-1;
if x='c' then
for i:=1 to N do
a[i,y]:=a[i,y]*-1;
end;
function Suma(a:matrice):int64;
var i,j:integer; S:int64;
begin
S:=0;
for i:=1 to N do
for j:=1 to M do
S:=S+a[i,j];
Suma:=S;
end;
Begin
Assign(F, 'flip.in');
reset(F);
readln(F, N,M);
for i:=1 to N do
begin
for j:=1 to M do
read(F, a[i,j]);
readln(F);
end;
close(F);
for i:=1 to N*M do begin
CautaCandidat;
Flip(x,y);
end;
assign(G, 'flip.out');
rewrite(G);
writeln(G, max);
close(G);
readln;
end.