Greedy algorithms
Greedy-20
Greedy-21
Geometry-01
~/algor $ cat problems/ochkoz-uchinchisi-bepul
Third one free
Medium
1000 ms
128 MB
45%
A shop runs a promotion: at the checkout items can be paid for in groups of three, and in every full group of three the cheapest item is free. You choose the groups yourself; items left outside full groups of three are paid at full price. The prices of n items are given. What is the smallest amount you have to pay to buy all of them?
Input
The first line contains n — the number of items. The second line contains n integers — the prices.
Output
One integer — the smallest amount to pay.
Note
1 ≤ n ≤ 200 000; 1 ≤ price ≤ 10⁹. The answer may not fit into a 32-bit integer.
Examples
Input #1
6 5 1 3 4 2 6
Output #1
16
Input #2
3 10 10 10
Output #2
20