Cod sursa(job #16222)

Utilizator radu_voroneanuVoroneanu Radu Stefan radu_voroneanu Data 12 februarie 2007 17:03:54
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.17 kb
var a:array[1..16,1..16] of longint;
    pozc,pozl,negl,negc:array[1..16] of int64;
    n,m,k,l:longint;
    s:int64;
    f,g:text;
procedure citesc;
 var i,j:longint;
 begin
  readln(f,n,m);
  for i:=1 to n do
   for j:=1 to m do begin
    read(f,a[i,j]);
    if a[i,j]<0 then begin
     inc(negl[i],a[i,j]);
     inc(negc[j],a[i,j]);
    end
    else begin
     inc(pozl[i],a[i,j]);
     inc(pozc[j],a[i,j]);
    end;
   end;
 end;
procedure schimb(x,y:longint);
 begin
  if a[x,y]<0 then begin
   negl[x]:=negl[x]-a[x,y];
   negc[y]:=negc[y]-a[x,y];
   a[x,y]:=a[x,y]*-1;
   pozl[x]:=pozl[x]+a[x,y];
   pozc[y]:=pozc[y]+a[x,y];
  end
  else begin
   pozl[x]:=pozl[x]-a[x,y];
   pozc[y]:=pozc[y]-a[x,y];
   a[x,y]:=a[x,y]*-1;
   negl[x]:=negl[x]+a[x,y];
   negc[y]:=negc[y]+a[x,y];
  end;
 end;
begin
 assign(f,'flip.in'); reset(f);
 assign(g,'flip.out'); rewrite(g);
 citesc;
 for k:=1 to n do
  if pozl[k]<abs(negl[k]) then
   for l:=1 to m do
    schimb(k,l);
 for k:=1 to m do
  if pozc[k]<abs(negc[k]) then
   for l:=1 to n do
    schimb(l,k);
 s:=0;
 for k:=1 to n do
  s:=s+pozl[k]+negl[k];
 writeln(g,s);
 close(f); close(g);
end.