greedy implementation