implementing greedy algorithm