The KSG convenience store runs a 2+1 sale on dairy products such as fruit milk and drinking yogurt. If you buy three dairy packs in one purchase, the cheapest of the three is free and you pay only for the other two. A purchase that does not contain exactly three packs gets no discount, so you pay the full price of every pack in it.
For example, suppose seven packs cost 10, 9, 4, 2, 6, 4 and 3, and Jaehyun buys them in three purchases: (10, 3, 2), (4, 6, 4) and (9). He pays 13 for the first purchase, 10 for the second and 9 for the third.
Jaehyun wants to buy all N packs to share with his friends. He may split the packs into purchases however he likes. Find the minimum total cost of buying all N packs.