finished greedy alg