3519.Buy One, Get One Free

Time Limit: 1s Memory Limit: 256MB

A nearby pie shop is having a special sale. For each pie you pay full price for, you may select one pie of a strictly lesser value to get for free. Given the prices of all the pies you wish to acquire, determine the minimum total amount you must pay for all of the pies.

Input Format(From the terminal/stdin)

Input will begin with an integer n (1 \le n \le 500000), the number of pies you wish to acquire. Following this is a line with n integers, each indicating the cost of a pie. All costs are positive integers not exceeding 109.

Output Format(To the terminal/stdout)

Print the minimum cost to acquire all the pies.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Sample Input 1

Copy
6
3 4 5 3 4 5
 \n
 · · · · · \n

Sample Output 1

Copy
14
  \n

Sample Input 2

Copy
5
5 5 5 5 5
 \n
 · · · · \n

Sample Output 2

Copy
25
  \n

Sample Input 3

Copy
4
309999 6000 2080 2080
 \n
      ·    ·    ·    \n

Sample Output 3

Copy
314159
      \n

Hints

In the first test case you can pay for a pie with cost 5 and get a pie with cost 4 for free, then pay for a pie with cost 5 and get a pie with cost 3 for free, then pay for a pie with cost 4 and get a pie with cost 3 for free.

In the second test case you have to pay full price for every pie.

Submit

请先 登录

© 2025 FAQs