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. css3 画三角形

    /*箭头向上*/ .arrow-up { width:0; height:0; border-left:20px solid transparent; border-right:20px solid ...

  2. 树莓派LED指示灯说明

    原文:http://shumeipai.nxez.com/2014/09/30/raspberry-pi-led-status-detail.html?variant=zh-cn LED亮灯状态 LE ...

  3. STM32 GPIO寄存器 IDR ODR BSRR BRR

    IDR是查看引脚电平状态用的寄存器,ODR是引脚电平输出的寄存器 下面内容的原文:http://m646208823.blog.163.com/blog/static/1669029532012931 ...

  4. 简化LINUX的命令输入 简化linux命令 快捷键 短路径

    在LINUX中,有很多常用的命令,常用的命令我们可以熟练的记忆,但是对于不经常使用的命令恐怕是需要翻阅手册了,但是我们可以简化这些命令的输入来达到简便记忆的效果. 这里以BSH为例: 编辑/etc/b ...

  5. UNIDAC如何驱动MSSQL2000

    UNIDAC如何驱动MSSQL2000 如下图,PROVIDER必须设置为PRSQL.默认的PRAUTO,如果操作系统是XP可以,如果是WIN7以上的,不可以. 原因是MSSQL NATIVE 11已 ...

  6. 基于centos 创建stress镜像——源码安装stress

    上一篇文章进行了yum安装stress,这次对stress进行源码编译安装,并且生成新的镜像 创建Dockerfile目录 [vagrant@localhost ~]$ mkdir -p /tmp/s ...

  7. redux 及 相关插件 项目实战

    目录结构 +-- app | +-- actions | +-- index.js | +-- components | +-- content.js | +-- footer.js | +-- se ...

  8. java中等待所有线程都执行结束(转)

    转自:http://blog.csdn.net/liweisnake/article/details/12966761 今天看到一篇文章,是关于java中如何等待所有线程都执行结束,文章总结得很好,原 ...

  9. js 日期 (10 + '').length == 10 ? '0' + 10 : 10;

    js 日期 (10 + '').length == 10 ? '0' + 10 : 10;

  10. Androidproject文件下assets目录与res目录的差别

    1. assets : 不会在R.java文件下生成对应的标记,assets目录能够自己创建目录,必须使用AssetsManager类进行訪问,存放到这里的资源在执行打包的时候都会打入程序安装包中, ...