#P6530. Bookstore Promotion

    ID: 19743 Type: Default 1000ms 256MiB

Bookstore Promotion

Bookstore Promotion

The bookstore is having a promotion! In this discount event, if you purchase exactly 3 books in one go, you only pay for the two more expensive ones, i.e. you get the cheapest book for free. Note that if you purchase only 1 or 2 books at a time, no discount applies.

You are given n books with their prices. You can buy the books in one or more transactions, and if a transaction has exactly 3 books, the discount is applied. Your goal is to purchase all n books while spending the minimum amount of money.

Hint: Sorting the prices in descending order and grouping every three books together will help you decide which book becomes free.

Formula (if you need it): If the sorted prices (in descending order) are p0, p1, p2, ..., then the minimum total cost is:

\[ \text{total cost} = \sum_{i=0}^{n-1} p_i - \sum_{j=2,5,8,...} p_j \]

inputFormat

The input is given in two lines:

  • The first line contains an integer n (the number of books).
  • The second line contains n space-separated positive integers representing the prices of the books.

outputFormat

Output the minimum amount of money required to purchase all n books.

sample

3
6 5 3
11