COCI '15 Contest 1 #2 Akcija

View as PDF

Submit solution

Points: 5
Time limit: 1.0s
Memory limit: 32M

Problem type

There is a promotional offer in a bookstore "Take 3, pay for the 2 more expensive ones". So, each customer who picks 3 books gets the cheapest one for free. Of course, the customer can take even more books and, depending on the way the books are arranged into groups of three, get the cheapest one in each group for free.

For example, let the prices of the books taken by the customer be: 10\ 3\ 2\ 4\ 6\ 4\ 9. If he arranges them into groups: (10, 3, 2), (4, 6, 4) and (9), he will get the books priced 2 from the first group for free and the book priced 4 from the second group. We can see that he won't get anything for free from the third group because it contains only one book.

The lady working in the bookstore is well-intentioned and she always wants to lower the price for each customer as much as possible. For given book prices, help the lady arrange the books into groups in the best way possible, so that the total price the customer has to pay is minimal.

Please note: The lady doesn't have to arrange the books into groups so that each group contains exactly 3 books, but the number of books in a group needs to be between 1 and 3, inclusively.

Input Specification

The first line of input contains the integer N (1 \le N \le 100\,000), the number of books the customer bought. Each of the following N lines contains a single integer C_i (1 \le C_i \le 100\,000), the price of each book.

In test cases worth 50% of points, it will hold that N \le 20.

Output Specification

The first and only line of output must contain the required minimal price.

Sample Input 1

4
3
2
3
2

Sample Output 1

8

Explanation for Sample Output 1

The lady can put the books priced 3, 2, 2 in one group, and only the book priced 3 in the other group, which is also the cheapest combination.

Sample Input 2

6
6
4
5
5
5
5

Sample Output 2

21

Explanation for Sample Output 2

The lady puts books priced 6, 4, 5 in one group, and 5, 5, 5 in the other, which gives us the cheapest combination.


Comments

There are no comments at the moment.