How to sort coins efficiently in Codeforces 876B Sorting the Coins problem?