Pagini recente » Cod sursa (job #1411758) | Cod sursa (job #304309) | Cod sursa (job #197361) | Cod sursa (job #1522194) | Cod sursa (job #59939)
Cod sursa(job #59939)
type matrice=array[1..16,1..16] of integer;
vector=array[1..16] of integer;
var a:matrice;
i,s,smax,j,m,n,ncl,ncc:integer;
nl,nc,kl,kc,x:longint;
ll,cc:vector;
f:text;
procedure adunare(var v:vector; var nc:integer);
var i,t,s:integer;
begin
t:=(v[1]+1)div 2;
v[1]:=(v[1]+1) mod 2;
i:=2;
while (i<=nc) and (t>0) do
begin
s:=v[i]+t;
v[i]:=s mod 2;
t:=s div 2;
inc(i);
end;
if t=1 then
begin
inc(nc);
v[nc]:=1;
end;
end;
begin
assign(f,'flip.in');
reset(f);
readln(f,n,m);
for i:=1 to n do
for j:=1 to m do
read(f,a[i,j]);
close(f);
s:=0;
for i:=1 to n do
for j:=1 to m do
inc(s,a[i,j]);
smax:=s;
nl:=1;nc:=1;
nl:=(nl shl n) -1 ;
nc:=(nc shl m) -1 ;
ncl:=1;
for kl:=0 to nl do
begin
ncc:=1;
for kc:=0 to nc do
begin
s:=0;
for i:=1 to n do
for j:=1 to m do
if not odd(ll[i]+cc[j])then s:=s+a[i,j]
else s:=s-a[i,j];
if s>smax then smax:=s;
adunare(cc,ncc);
end;
adunare(ll,ncl);
end;
assign(f,'flip.out');
rewrite(f);
write(f,smax);
close(f);
end.