Question: An error message from a backtracking procedure

I'm experimenting with a backtracking procedure to solve the subset-sum problem: given a list L of integers, and an integer N not in L, to see if elements of L can be found which sum to N.  Basically the procedure works recursively, adding elements to the current list until either the sum equals N, in which case we stop with a solution, or the sum exceeds N, in which case we go back one level: 

subsetsum:=proc(X,Y,N) # X is a set, N is an integer
  local y;
if (add(x,x=Y)<=N) then
  if (add(x,x=Y)=N) then
      print(Y,`is a solution.`);
      break;
    else
      for y in (X minus Y) do
        if (add(x,x=(Y union {y}))<=N) then
          subsetsum(X,Y union {y},N);
        end if;
      end do;
  end if;
end if;
return();
end;

Now this procedure works well enough:

   subsetsum({1,2,4,7,10,11},{},25}

produces

  {4,10,11} is a solution

as well as the error message:

  Error, (in subsetsum) break or next outside do loop

This is what I don't understand - why do I obtain this error, and how can I alter my procedure to eliminate it?  I mean, I do want to stop if and when N is reached, so how do I break nicely?

Thanks,

Alasdair

Please Wait...