D. Boxes And Balls

time limit per test2 seconds

memory limit per test256 megabytes

题目链接:http://codeforces.com/contest/884/problem/D

Description

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:

  1. 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;
  2. 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.

题目精简之后可以看成一个sum分解成一个数列,每次分解的代价就是当前的sum,其实仔细想想就是一个3叉哈夫曼树,当不能凑成三叉的哈夫曼树的时候可以添加0来凑数(不会改变sum)。


哈夫曼树详解:https://www.cnblogs.com/mcgrady/p/3329825.html


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
priority_queue <ll,vector<ll>,greater<ll> > qu;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
ll now;
scanf("%lld",&now);
qu.push(now);
}
if(qu.size()%2 == 0)
qu.push(0);
ll ans = 0,a,b,c;
while(qu.size() > 1)
{
a = qu.top(); qu.pop();
b = qu.top(); qu.pop();
c = qu.top(); qu.pop();
ans += a + b + c;
qu.push(a+b+c);
}
printf("%lld",ans);
return 0;
}

Educational Codeforces Round 31- D. Boxes And Balls的更多相关文章

  1. Educational Codeforces Round 34 C. Boxes Packing【模拟/STL-map/俄罗斯套娃】

    C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  2. Educational Codeforces Round 31 B. Japanese Crosswords Strike Back【暴力】

    B. Japanese Crosswords Strike Back time limit per test 1 second memory limit per test 256 megabytes ...

  3. Educational Codeforces Round 31 A. Book Reading【暴力】

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. Educational Codeforces Round 31

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. 【Educational Codeforces Round 31 C】Bertown Subway

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后肯定会形成若干个环的. 把最大的两个环合在一起就好. 每个环贡献: 假设x=环的大小 ->x*x 注意int的溢出 [代码 ...

  6. 【Educational Codeforces Round 31 B】Japanese Crosswords Strike Back

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 所有数字的和加上n-1,如果为x则唯一,否则不唯一 [代码] #include <bits/stdc++.h> usin ...

  7. 【Educational Codeforces Round 31 A】Book Reading

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

  8. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  9. Educational Codeforces Round 32

    http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...

  10. Educational Codeforces Round 34 (Rated for Div. 2) A B C D

    Educational Codeforces Round 34 (Rated for Div. 2) A Hungry Student Problem 题目链接: http://codeforces. ...

随机推荐

  1. MODBUS移植的参考文章

    https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32 http://www.360doc.com/content/14/0906/09 ...

  2. odoo9 部署步详细步骤

    sudo apt-get updatesudo apt-get dist-upgrade 一:安装和配置pg sudo apt-get install  postgresql sudo su - po ...

  3. Nacos部署中的一些常见问题汇总

    开个帖子,汇总一下读者经常提到的一些问题 问题一:Ubuntu下启动Nacos报错 问题描述 使用命令sh startup.sh -m standalone启动报错: ./startup.sh: 78 ...

  4. mongodb 文本索引

    启用文本搜索: 最初文本搜索是一个实验性功能,但2.6版本开始,配置是默认启用的.但是,如果使用的是以前 MongoDB 的版本,那么必须启用文本搜索,使用下面的代码: >db.adminCom ...

  5. codeforces1025

    hackforces + fstforces A 很明显当有一个字母出现次数>1时即合法 $n = 1$的情况需要特判 #include<cstdio> #include<ve ...

  6. JS权威指南-概述学习

    <script src="/javascripts/application.js" type="text/javascript" charset=&quo ...

  7. PaaS基础学习(1)

    PaaS基础学习(1) PaaS学习笔记目录 PaaS基础学习(1) 在PaaS上开发Web.移动应用(2) PaaS优点与限制(3) 1. 基础单元,一个基础单元就是所研究实体的最小的不可分割的单元 ...

  8. path与classpath区别(转)

    转自http://blog.csdn.net/mydreamongo/article/details/8155408 1.path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中 ...

  9. Git 学习教程【转+总结】

    之前是在用SVN,现在因为小伙伴比较喜欢Git,所以也开始学习Git,很感谢 时光穿梭机 - 廖雪峰 的无私奉献.本文用来记录我在学习Git过程中的收获和笔记,廖雪峰大神的Git教程参考这里. 1.G ...

  10. MySQL主从复制原理介绍

    1)在mysql主库上,将改变记录到二进制日志(binary log)中. 2)在mysql从库上,IO线程将mysql主库上二进制日志(binary log)复制到中继日志(replay log)中 ...