min cost, no recursion version

min cost, no recursion version