See the solution to the Magic W problem from May 2003