Boxes And Balls (Codeforces 884D)
Tags: Greedy, data structures

Ivan has n different boxes. The first of them contains some balls of n different colors.Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i.In order to do this, Ivan will make some turns. Each turn he does the following:  Ivan chooses any non-empty box and takes all balls from this box;  Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3. The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.Help Ivan to determine the minimum possible penalty of the game!

Input: The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of boxes and colors.The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of balls with color i.

Output: Print one number — the minimum possible penalty of the game.

Note: In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.In the second example you make two turns:   Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 — to the fourth box and the rest put back into the first box. Penalty is 14;  Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 — to the second box. Penalty is 5. Total penalty is 19.

Sample Inputs:
3
1 2 3

4
2 3 4 5

Sample Outputs:
6

19

Solution (Python):
from heapq import *
n = int(input())
a = list(map(int, input().split()))
s = 0
if len(a) % 2 == 0:
    a = [0] + a
    n += 1

def sc(a, s=0):
    heapify(a)
    for i in range((n - 1) // 2):
        k = heappop(a) + heappop(a) + heappop(a)
        s += k
        heappush(a, k)
    print(s)
sc(a)

Solution (Python):
from heapq import *

def score(A):
    heapify(A)
    res = 0
    if len(A) % 2 == 0:
        (a0, a1) = (heappop(A), heappop(A))
        a = a0 + a1
        res += a
        heappush(A, a)
    while len(A) > 1:
        (a0, a1, a2) = (heappop(A), heappop(A), heappop(A))
        a = a0 + a1 + a2
        res += a
        heappush(A, a)
    return res

def main():
    n = int(input())
    A = list(map(int, input().split()))
    print(score(A))
main()

Solution (Python):
import heapq
n = int(input())
a = [int(i) for i in input().split()]
heapq.heapify(a)
if n & 1 == 0:
    heapq.heappush(a, 0)
res = 0
while len(a) > 1:
    s = heapq.heappop(a) + heapq.heappop(a) + heapq.heappop(a)
    res += s
    heapq.heappush(a, s)
print(res)