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 ≤ ni-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:

  1. Ivan chooses any non-empty box and takes all balls from this box;
  2. 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 a1a2, ..., an (1 ≤ ai ≤ 109), where aiis the number of balls with color i.

Output

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

Example

Input
3
1 2 3
Output
6
Input
4
2 3 4 5
Output
19

题意:一共N小堆石子,每堆有自己的重量,开始都合在一大堆。现在问这样可以让他们分成N小堆。具体的,每次可以选择一个大的堆(其重量为代价),分为2或者3个堆。

思路:逆向考虑就是石子合并:若干堆石子,都有自己的重量,每一次可以选2或者3堆合成一堆,每次的代价为所选石子重量和。

对于此类题型,我们考虑到合并的次数是一定的,所以我们希望重量轻的其“凑次数”,即是把轻的优先合并,然后把合并的结果再拿去排序,等待下一次合并。

对于此题:显然能分为3个堆的情况优于分为2个堆(次数少一些),其他的与普通的合并石子无差。 然后就是考虑到每次减少2堆,最后要留1堆,所以对于偶数堆还应该插入“0”。

#include<queue>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=;
priority_queue<ll,vector<ll>,greater<ll> >q;
ll a[maxn];
int main()
{
int N,i,j; ll ans=,tmp;
scanf("%d",&N);
for(i=;i<=N;i++) {
scanf("%lld",&a[i]);
q.push(a[i]);
}
if(N==) {
printf("0\n");
return ;
}
if(N%==) q.push();
while(q.size()>){
tmp=q.top(); q.pop();
tmp+=q.top(); q.pop();
tmp+=q.top(); q.pop();
ans+=tmp; q.push(tmp);
}
while(!q.empty()){
ans+=q.top(); q.pop();
}
printf("%lld\n",ans);
return ;
}

CodeForces-884D:Boxes And Balls(合并石子)的更多相关文章

  1. Educational Codeforces Round 31- D. Boxes And Balls

    D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...

  2. 【CF884D】Boxes And Balls 哈夫曼树

    [CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球 ...

  3. UESTC 886 方老师金币堆 --合并石子DP

    环状合并石子问题. 环状无非是第n个要和第1个相邻.可以复制该行石子到原来那行的右边即可达到目的. 定义:dp[i][j]代表从第i堆合并至第j堆所要消耗的最小体力. 转移方程:dp[i][j]=mi ...

  4. dp优化-四边形不等式(模板题:合并石子)

    学习博客:https://blog.csdn.net/noiau/article/details/72514812 看了好久,这里整理一下证明 方程形式:dp(i,j)=min(dp(i,k)+dp( ...

  5. 合并石子(dp)

    合并石子 时间限制: 1 Sec  内存限制: 128 MB提交: 7  解决: 7[提交][状态][讨论版][命题人:quanxing] 题目描述 在一个操场上一排地摆放着N堆石子.现要将石子有次序 ...

  6. Java实现 蓝桥杯 算法提高 合并石子

    算法提高 合并石子 时间限制:2.0s 内存限制:256.0MB 问题描述 在一条直线上有n堆石子,每堆有一定的数量,每次可以将两堆相邻的石子合并,合并后放在两堆的中间位置,合并的费用为两堆石子的总数 ...

  7. Boxes And Balls(三叉哈夫曼编码)

    题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...

  8. CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT

    原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...

  9. ny737 石子合并(一) 总结合并石子问题

    描述: 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将n堆石子合并 ...

随机推荐

  1. SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp

    题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...

  2. MVC模式(三层架构模式)

    (Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). MVC模式最早由Try ...

  3. Django 中的时间处理

    操作系统为OS X 10.9.2,Django为1.6.5. 1.时区 在setting.py文件中设置了时区 TIME_ZONE = 'Asia/Shanghai' # 设置时区为UTC+8 USE ...

  4. Spring 详解(二)------- AOP关键概念以及两种实现方式

    目录 1. AOP 关键词 2. AOP 的作用 3. AOP 的通知类型 4. 基于 xml 的配置方式 5. 基于注解的配置方式 6. 切面的优先级 7. 重用切点表达式 8. 两种方式的比较(摘 ...

  5. 转 vs2010转vs2008 其他的一样

    如果你使用VS2010的任何版本写代码,那么在VS2008中就不能打开VS2010的解决方案了,为此,通过以下三步就可以解决了一.对于工程名.sln; 1.用你喜欢的编辑器打开sln文件,比如note ...

  6. 关于克隆gitlab项目的一些 问题列表

    1. gitLab项目clone过后, 运行ng serve出错: 解决方式: npm install -g node-gyp npm install --global --production wi ...

  7. mysql统计功能和数据库information_schema/performance_schema

    1.去重统计数据表行数: select count(distinct col_name) from table_name; 2.统计行数 select count(*) from table_name ...

  8. Linux索引节点(Inode:no space for device)用满导致的一次故障

    问题描写叙述 在storm測试环境集群上上nimbus和supervisor自己主动挂调.重新启动时显示no space for device,也不能创建,加入文件及文件夹,df -h查看 ilesy ...

  9. [Oracle] 获取运行计划的各方法总结

    总的结论: 一.获取运行计划的6种方法(具体步骤已经在每一个样例的开头凝视部分说明了): 1. explain plan for获取:  2. set autotrace on .  3. stati ...

  10. UBUntu 软件 源配置方法

        近期公司产品须要添加一个功能,就是版本号自己主动更新.使用apt-get 实现. apt-get 软件源配置的方法,參见本人资源里的共享.以下是代码中作为升级的一部分.  FILE *fp; ...