T
tdKno
Unregistered / Unconfirmed
GUEST, unregistred user!
function FindMaxSum(A:IA;Sum:Integer):Integer;
var
MinLeft:Integer;
Used:array of Boolean;
procedure FindLeft(LeftSum,LeftCount:Integer);
var
i,n,s:Integer;
Str:String;
begin
if (A[0]>LeftSum) or Used[0] then
begin
if LeftSum<MinLeft then
begin
MinLeft:=LeftSum;
Str:='';
s:=0;
n:=0;
for i:=0 to High(A)do
if Used then
begin
Str:=Str+IntToStr(A)+' ';
Inc(s,A);
Inc;
end;
//
if (LeftSum=0) or (n>=High(A)) then
Abort;
end;
exit;
end;
n:=LeftCount;
while n>=0do
begin
if A[n]>LeftSum then
Dec
else
break;
end;
for i:=ndo
wnto 0do
begin
Used:=true;
FindLeft(LeftSum-A,i-1);
Used:=false;
end;
end;
begin
SetLength(Used,High(A)+1);
FillChar(Used[0],(High(A)+1)*SizeOf(Boolean),0);
MinLeft:=Sum;
try
FindLeft(Sum,High(A));
except
end;
Result:=Sum-MinLeft;
end;
var
MinLeft:Integer;
Used:array of Boolean;
procedure FindLeft(LeftSum,LeftCount:Integer);
var
i,n,s:Integer;
Str:String;
begin
if (A[0]>LeftSum) or Used[0] then
begin
if LeftSum<MinLeft then
begin
MinLeft:=LeftSum;
Str:='';
s:=0;
n:=0;
for i:=0 to High(A)do
if Used then
begin
Str:=Str+IntToStr(A)+' ';
Inc(s,A);
Inc;
end;
//
if (LeftSum=0) or (n>=High(A)) then
Abort;
end;
exit;
end;
n:=LeftCount;
while n>=0do
begin
if A[n]>LeftSum then
Dec
else
break;
end;
for i:=ndo
wnto 0do
begin
Used:=true;
FindLeft(LeftSum-A,i-1);
Used:=false;
end;
end;
begin
SetLength(Used,High(A)+1);
FillChar(Used[0],(High(A)+1)*SizeOf(Boolean),0);
MinLeft:=Sum;
try
FindLeft(Sum,High(A));
except
end;
Result:=Sum-MinLeft;
end;