ural 1014. Greedy algorithm