Cod sursa(job #255240)

Utilizator ioalexno1Alexandru Bunget ioalexno1 Data 8 februarie 2009 21:41:56
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.73 kb
program alex;
var f:text;
    a:array[1..20,1..20]of longint;
    st:array[0..100]of longint;
    i,j,m,n,h,q:longint;
    s,s1,max,suma:longint;
    as:boolean;
begin
assign(f,'flip.in');reset(f);
readln(f,m,n);
for i:=1 to m do
    begin
    for j:=1 to n do
        read(f,a[i,j]);
    readln(f);
    end;
close(f);
max:=-2000000000;
j:=1;
st[j]:=-1;
while j>0 do
      begin
      if st[j]<1 then begin
                      st[j]:=st[j]+1;
                      as:=true;
                      end
                  else as:=false;
      if as then begin
                 if j=m then begin
                             for i:=1 to m do
                                 if st[i]=0 then for h:=1 to n do
                                                     a[i,h]:=a[i,h]*(-1);
                             suma:=0;
                             for i:=1 to n do
                                 begin
                                 s:=0;
                                 for h:=1 to m do
                                     s:=s+a[h,i];
                                 if s>0 then suma:=suma+s
                                         else suma:=suma-s;
                                 end;
                             if suma>max then max:=suma;
                             end
                          else begin
                               j:=j+1;
                               st[j]:=-1;
                               end;
                 end
            else begin
                 j:=j-1;
                 if st[j]=0 then for i:=1 to n do
                                     a[j,i]:=a[j,i]*(-1);
                 end;
      end;
assign(f,'flip.out');rewrite(f);
writeln(f,max);
close(f);
end.