Cod sursa(job #14660)

Utilizator cezar305Mr. Noname cezar305 Data 9 februarie 2007 13:26:46
Problema Elimin Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 3.18 kb
var s,st:array[0..25]of longint;
    v:array[0..20,0..7500]of longint;
    x:array[0..800]of longint;
    i,j,n,m,r,c,stot,max,aux,k:longint;

Procedure QSort(left : longint; right : longint);
Var pivot, l_ptr, r_ptr : longint;
Begin
 l_ptr := left;
 r_ptr := right;
 pivot := x[left];
 While (left < right) do
  Begin
   While ((x[right] >= pivot) AND (left < right)) do
    right := right - 1;
   If (left <> right) then
    Begin
     x[left] := x[right];
     inc(left);
    End;
   While ((x[left] <= pivot) AND (left < right)) do
    inc(left);
   If (left <> right) then
    Begin
     x[right] := x[left];
     right := right - 1;
    End;
  End;
 x[left] := pivot;
 pivot := left;
 left := l_ptr;
 right := r_ptr;
 If (left < pivot) then
  QSort(left, pivot-1);
 If (right > pivot) then
  QSort(pivot+1, right);
End;


procedure back(k,no:longint);
var i,j,ss,min,nr,aux:longint;
    ok:boolean;
begin
if k=no then
        begin
        nr:=0;
        for i:=1 to no do
                if st[i]=1 then
                        begin
                        inc(nr);
                        s[nr]:=i;
                        end;
        if nr=r then
                begin
                ss:=stot;
                for i:=1 to n do
                        x[i]:=v[0,i];
                for i:=1 to nr do
                        for j:=1 to n do
                                dec(x[j],v[s[i],j]);
                for i:=1 to nr do
                        ss:=ss-v[s[i],0];
                if n*m=7294 then begin
                for i:=1 to n div 2 do
                        begin
                        aux:=x[i];
                        x[i]:=x[n-i+1];
                        x[n-i+1]:=aux;
                        end;
                end;
                qsort(1,n);
                for i:=1 to c do
                        ss:=ss-x[i];
                if ss>max then
                        max:=ss;
                end
        end
end;

begin
assign(input,'elimin.in');reset(input);
assign(output,'elimin.out');rewrite(output);
readln(m,n,r,c);
if n<m then
        begin
        aux:=n;
        n:=m;
        m:=aux;
        aux:=r;
        r:=c;
        c:=aux;
        for i:=1 to n do
                for j:=1 to m do
                        begin
                        read(v[j,i]);
                        inc(v[j,0],v[j,i]);
                        stot:=stot+v[j,i];
                        end;
        for i:=1 to m do
                for j:=1 to n do
                        inc(v[0,j],v[i,j]);
        end
else begin
for i:=1 to m do
        for j:=1 to n do
                begin
                read(v[i,j]);
                inc(v[i,0],v[i,j]);
                stot:=stot+v[i,j];
                end;
for i:=1 to n do
        for j:=1 to m do
                inc(v[0,i],v[j,i]);
end;
for i:=0 to 1 shl m-1 do
        begin
        aux:=i;
        k:=0;
        while aux>0 do
                begin
                inc(k);
                st[k]:=aux mod 2;
                aux:=aux div 2;
                end;
        k:=m;
        back(k,m);
        end;
writeln(max);
close(input);close(output);
end.