Cod sursa(job #137327)

Utilizator razvan_emPrecupas Razvan razvan_em Data 17 februarie 2008 11:16:14
Problema Stalpi Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda 4, Clasa a 10-a Marime 1.38 kb
program stalpi;
var f,g:text;
    n,con,y,min:word;
    st,x,c,s,d,v:array [1..1000] of word;

procedure initializari;
var i:word;
begin
for i:=1 to n do st[i]:=0;
end;

procedure tipar(p:word);
var i,suma:word;
begin
y:=y+1;
suma:=0;
for i:=1 to p do
suma:=suma+c[st[i]];
v[y]:=suma;
end;

function verificare(p:word):boolean;
var cont,l,k:word;
begin
k:=0;
for cont:=1 to p do
for l:=1 to n do
if x[st[cont]]=x[l] then k:=k+1
else if x[st[cont]]>x[l] then
begin
if (x[st[cont]]-s[st[cont]]<=x[l]) then k:=k+1;
end
else if (x[st[cont]]+d[st[cont]]>=x[l]) then k:=k+1;
if k>=n then verificare:=true
else verificare:=false;
end;

function validare(p:word):boolean;
var i:word; ok:boolean;
begin
ok:=true;
for i:=1 to p-1 do
if st[p]=st[i] then ok:=false;
validare:=ok;
end;

procedure tipar1(p:word);
var j:word;
begin
for j:=1 to p do write(g,st[j]);
writeln(g);
end;

procedure bktr(p:word);
var pval:word;
begin
for pval:=1 to n do
begin
st[p]:=pval;
if validare(p) and verificare(p) then tipar(p);
if validare(p) and (p<n) then bktr(p+1);
end; end;

begin
assign(f,'stalpi.in'); reset(f);
assign(g,'stalpi.out'); rewrite(g);
readln(f,n);
for con:=1 to n do
readln(f,x[con],c[con],s[con],d[con]);
initializari;
y:=0;
bktr(1);
min:=maxint;
for con:=1 to y do
if v[con]<=min then min:=v[con];
write(g,min);
close(f); close(g);
end.