Least coins algorithm from CSC321