function GetSum(const S: Integer;
const W: array of Integer): Boolean;
const
BufLen = 10000;
type
TNode = record
Sum: Integer;
d: Integer;
Father: Integer
end;
var
List: array [1..BufLen] of TNode;
Close, Open: Integer;
i, k: Integer;
Sum: Integer;
Len: Integer;
begin
Len:=Length(W);
FillChar(List[1], SizeOf(TNode), 0);
Close:=0;
Open:=1;
repeat
Inc(Close);
for i:=List[Close].d+1 to Lendo
begin
Sum:=List[Close].Sum+W[i-1];
if Sum=S then
begin
Write(S, '=', W[i-1]);
k:=Close;
while k<>1do
begin
Write('+', W[List[k].d-1]);
k:=List[k].Father
end;
Result:=True;
Exit
end
else
if (Sum<S) and (Open<BufLen) then
begin
Inc(Open);
List[Open].Sum:=Sum;
List[Open].d:=i;
List[Open].Father:=Close
end
end
until (Close=Open) or (Open=BufLen);
Result:=False
end;