Cod sursa(job #11068)

Utilizator ScrazyRobert Szasz Scrazy Data 30 ianuarie 2007 14:08:26
Problema Jocul Flip Scor 20
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.02 kb
var
ertek:longint;
be,ki:text;
i,j:word;
n,m:byte;
matr:array[1..16,1..16]of longint;
px,py,x,y:array[1..16]of longint;
nx,ny:array[1..16]of longint;

begin
assign(be,'flip.in');
reset(be);
readln(be,n,m);
for i:=1 to n do begin
  for j:=1 to m do begin
    read(be,matr[i,j]);
    if matr[i,j]>=0 then px[i]:=px[i]+matr[i,j]
    else nx[i]:=nx[i]+abs(matr[i,j]);
  end;
  if nx[i]>px[i] then begin x[i]:=nx[i]-px[i];
                      for j:=1 to m do
                        matr[i,j]:=-matr[i,j];
  end
  else x[i]:=px[i]-nx[i];
readln(be);
end;
close(be);

for i:=1 to n do begin
  for j:=1 to n do begin
    if matr[j,i]>=0 then py[i]:=py[i]+matr[j,i]
    else ny[i]:=ny[i]+abs(matr[j,i]);
  end;
  if ny[i]>py[i] then begin y[i]:=ny[i]-py[i];
                      for j:=1 to m do
                        matr[j,i]:=-matr[j,i];
  end
  else y[i]:=py[i]-ny[i];
end;
ertek:=0;

for i:=1 to m do
 ertek:=ertek+y[i];
assign(ki,'flip.out');
rewrite(ki);
write(ki,ertek);
close(ki);
end.