Pagini recente » Cod sursa (job #2182355) | Cod sursa (job #2549151) | Cod sursa (job #637229) | Cod sursa (job #793988) | Cod sursa (job #1552886)
var f:text;
k,n,m,i,j,l,o,r,c:byte;
sch:array[1..3]of record
lc,care:byte;
end;
a:array[1..16,1..16]of longint;
s,sc,max:longint;
pot_iesi:boolean;
begin
randomize;
assign(f,'flip.in');
reset(f);s:=0;
readln(f,n,m);
for i:=1 to n do
for j:=1 to m do
begin
read(f,a[i,j]);
inc(s,a[i,j])
end;
close(f);
max:=-maxlongint;
r:=0;c:=random(10)+1;
repeat
pot_iesi:=false;k:=0;
{caut linia cu flip maxim}
for i:=1 to n do
begin
sc:=s;
for j:=1 to m do
sc:=sc-a[i,j]+(a[i,j]*(-1));
if sc>max then
begin
max:=sc;
k:=1;
sch[k].lc:=0;
sch[k].care:=i;
end;
end;
{caut coloana cu flip maxim}
for j:=1 to m do
begin
sc:=s;
for i:=1 to n do
sc:=sc-a[i,j]+(a[i,j]*(-1));
if sc>max then
begin
max:=sc;
k:=1;
sch[k].lc:=1;
sch[k].care:=j;
end;
end;
{caut combinatia linie-coloana cu flip maxim}
for i:=1 to n do
for j:=1 to m do
begin
sc:=s;
for l:=1 to m do
sc:=sc-a[i,l]+(a[i,l]*(-1));
for l:=1 to n do
sc:=sc-a[l,j]+(a[l,j]*(-1));
sc:=sc+4*(a[i,j]);
if sc>max then
begin
max:=sc;
k:=1;
sch[k].lc:=0;
sch[k].care:=i;
inc(k);
sch[k].lc:=1;
sch[k].care:=j;
end;
end;
if r=c then pot_iesi:=true;
if max<=s then {pot_iesi:=true}inc(r);
{else}
begin
s:=max;
for i:=1 to k do
case sch[i].lc of
0:
begin
for j:=1 to m do
a[sch[i].care,j]:=-a[sch[i].care,j]
end;
1:
begin
for j:=1 to n do
a[j,sch[i].care]:=-a[j,sch[i].care]
end;
end;
end;
until pot_iesi;
assign(f,'flip.out');
rewrite(f);
writeln(f,max);
close(f);
end.