Cod sursa(job #12589)

Utilizator andrei_infoMirestean Andrei andrei_info Data 4 februarie 2007 14:28:28
Problema Elimin Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.69 kb
//infoarena elimin preoni 2007 runda 1

var a:array[1..15,1..600] of integer;
    suma:array[1..600] of longint;
    x:array[1..15] of integer;
    n,m,r,c,s:integer;
    rez:longint;

procedure citire;
var i,j,aux:integer;
begin
assign(input,'elimin.in'); reset(input);
readln(m,n,r,c);
if m <= 15 then
        begin
        for i:=1 to m do
                   for j:=1 to n do
                        read(a[i,j]);
        end
else
        begin
        aux:=r; r:=c; c:=aux;
        for j:=1 to m do
                for i:=1 to n do
                        read(a[i,j]);
        aux:=m; m:=n; n:=aux;
        end;
close(input);
end;

procedure calc;
var i,j:integer;
    aux,rr:longint;
begin
fillchar(suma,sizeof(suma),0);
for i:=1 to m do
         for j:=1 to n do
                if x[i] = 1 then suma[j]:=suma[j]+a[i,j];

for i:=1 to m-1 do
        for j:=i+1 to m do
                if suma[i] < suma[j] then
                        begin
                        aux:=suma[i];
                        suma[i]:=suma[j];
                        suma[j]:=aux;
                        end;
rr:=0;
for i:=1 to m-c do
        rr:=rr+suma[i];
if rr > rez then rez:=rr;

end;



procedure back(k:byte);
var i:integer;
begin
if k = m+1 then
        begin
   if s = r then
        calc
        end
else
        for i:=0 to 1 do
                begin
                x[k]:=i;
                if i = 0 then inc(s);
                if s <= r then
                         back(k+1);
                if i = 0 then dec(s);
                end;
end;


begin
citire;
back(1);
assign(output,'elimin.out'); rewrite(output);
writeln(rez);
close(output);

end.