A11441 | Boxes And Balls
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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:
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!
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:
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!
输入格式
The first line contains one integer number $n$ ( $1<=n<=200000$ ) — the number of boxes and colors.
The second line contains $n$ integer numbers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ ( $1<=a_{i}<=10^{9}$ ), where $a_{i}$ is the number of balls with color $i$ .
The second line contains $n$ integer numbers $a_{1}$ , $a_{2}$ , ..., $a_{n}$ ( $1<=a_{i}<=10^{9}$ ), where $a_{i}$ is the number of balls with color $i$ .
输出格式
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=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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted