Cod sursa(job #195075)

Utilizator theratmantheratman theratman Data 16 iunie 2008 14:50:59
Problema Jocul Flip Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.07 kb
var f,g:text;
    l,c:array[1..16]of int64;
    a:array[1..16,1..16]of longint;
    i,j,n,m,h:longint;
    s:qword;

function ok:boolean;
         var i:longint;
         begin
         ok:=true;
         for i:=1 to n do
             if l[i]<0 then ok:=false;
         for i:=1 to m do
             if c[i]<0 then ok:=false;
         end;     a
begin
assign(f,'flip.in');reset(f);
assign(g,'flip.out');rewrite(g);
read(f,n,m);
for i:=1 to n do
    for j:=1 to m do begin
        read(f,a[i,j]);
        l[i]:=l[i]+a[i,j];
        c[j]:=c[j]+a[i,j];
        end;
repeat
for i:=1 to n do
    if l[i]<0 then begin
       l[i]:=l[i]*-1;
       for h:=1 to m do
           a[i,h]:=a[i,h]*-1;
       for j:=1 to m do inc(c[j],2*a[i,j]);
       end;
for i:=1 to m do
    if c[i]<0 then begin
       c[i]:=c[i]*-1;
       for h:=1 to n do
           a[h,i]:=a[h,i]*-1;
       for j:=1 to n do inc(l[j],2*a[j,i]);
       end;
until ok;
s:=0;
for i:=1 to n do
    s:=s+l[i];
for i:=1 to m do
    s:=s+c[i];
writeln(g,s div 2);
close(f);
close(g);
end.