greedy algorithm works