CF884D.Boxes And Balls

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Ivan has nn different boxes. The first of them contains some balls of nn different colors.

Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every ii ( 1<=i<=n1<=i<=n ) ii -th box will contain all balls with color ii .

In order to do this, Ivan will make some turns. Each turn he does the following:

  1. Ivan chooses any non-empty box and takes all balls from this box;
  2. Then Ivan chooses any kk 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 kk 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=2k=2 or k=3k=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!

输入格式

The first line contains one integer number nn ( 1<=n<=2000001<=n<=200000 ) — the number of boxes and colors.

The second line contains nn integer numbers a1a_{1} , a2a_{2} , ..., ana_{n} ( 1<=ai<=1091<=a_{i}<=10^{9} ), where aia_{i} is the number of balls with color ii .

输出格式

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

输入输出样例

  • 输入#1

    3
    1 2 3
    

    输出#1

    6
    
  • 输入#2

    4
    2 3 4 5
    

    输出#2

    19
    

说明/提示

In the first example you take all the balls from the first box, choose k=3k=3 and sort all colors to corresponding boxes. Penalty is 66 .

In the second example you make two turns:

  1. Take all the balls from the first box, choose k=3k=3 , put balls of color 33 to the third box, of color 44 — to the fourth box and the rest put back into the first box. Penalty is 1414 ;
  2. Take all the balls from the first box, choose k=2k=2 , put balls of color 11 to the first box, of color 22 — to the second box. Penalty is 55 .

Total penalty is 1919 .

首页