Cod sursa(job #254813)

Utilizator bodyionitaIonita Bogdan Constantin bodyionita Data 7 februarie 2009 17:41:49
Problema Jocul Flip Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 2.03 kb
    program flip;  
    var f:text;  
        i,j,k,n,m,max,s,s1,s2:longint;  
        a:array[1..16,1..16] of longint;  
        st:array[1..50] of longint;  
        as,ev:boolean;  
    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);  
     max:=-2000000000;  
     j:=1;  
     st[j]:=0;  
     while j>0 do  
     begin  
           if st[j]<2 then begin  
                             st[j]:=st[j]+1;  
                             as:=true  
                            end  
                        else as:=false;  
         if as then if j=n then begin  
                                   for i:=1 to n do  
                                     if st[i]=2 then  
                                       for k:=1 to m do a[i,k]:=-a[i,k];  
                                   s:=0;  
                                   for k:=1 to m do  
                                   begin  
                                    s1:=0;  
                                    s2:=0;  
                                    for i:=1 to n do  
                                      if a[i,k]>0 then s1:=s1+a[i,k]  
                                                  else s2:=s2-a[i,k];  
                                    if s2>s1 then s:=s+s2-s1  
                                             else s:=s+s1-s2;  
                                   end;  
     
                                   if s>max then max:=s;  
                                  end  
                             else begin  
                                   j:=j+1;  
                                   st[j]:=0  
                                 end  
               else begin  
                       j:=j-1;  
                       if st[j]=2 then for k:=1 to m do a[j,k]:=-a[j,k]  
     
                    end;  
       end;  
       assign(f,'flip.out');  
       rewrite(f);  
       writeln(f,max);  
       close(f);  
   end.