#P6530. [COCI 2015/2016 #1] AKCIJA

[COCI 2015/2016 #1] AKCIJA

Description

The bookstore is running a promotion.

Now, you can buy 33 books at once, and among the three books, you only need to pay for the two more expensive ones.

Note that this discount does not apply when buying 11 or 22 books at once.

Now, you want to spend as little money as possible to buy nn books.

Please output the minimum total cost to buy all nn books.

Input Format

The first line contains an integer nn.

The next nn lines each contain an integer cic_i. The ii-th line gives the price of the ii-th book.

Output Format

Output a single integer, representing the minimum total cost to buy nn books.

4
3
2
3
2 

8
6
6
4
5
5
5
5

21

Hint

Sample Explanation

Sample 1 Explanation

Buy the three books priced 3,2,23,2,2 together, and then buy the remaining book separately.

Sample 2 Explanation

Buy the three books priced 6,4,56,4,5 together, and then buy the three books priced 5,5,55,5,5 together.

Constraints

  • For 50%50\% of the testdata, n2×103n\le 2\times 10^3 is guaranteed.
  • For 100%100\% of the testdata, 1n1051\le n\le 10^5 and 1ci1051\le c_i\le 10^5 are guaranteed.

Notes

This problem is worth 8080 points in total.

Translated from Croatian Open Competition in Informatics 2015/2016 Contest #1 T2 AKCIJA.

Translated by ChatGPT 5