CodeForces - 884D Boxes And Balls

Boxes And Balls

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.

Examples

Input

3
1 2 3

Output

6

Input

4
2 3 4 5

Output

19

本题题意:有n种球n个盒子,给n个数代表第i种颜色的求有多少个,初始时所有的球都在一个盒子中,现要将第i个颜色的球放入第i个盒子中,每次只能选择一个非空盒子,然后将其中所有球拿出来,然后再选k个空盒子,将所有的球全部任意分配至这k个盒子中,每次消耗拿出球的个数。求最小的消耗。

解题思路:我们可以逆向推,看做将n个数合并,每次都要满足k=3,且每次放入三个最小的,才能满足消耗最小,发现当n为奇数时可以满足,如果n不为奇数则加一个0进去,即可满足,用哈夫曼树求解。

AC代码如下:

#include<iostream>
#include<queue>
#include<functional>
using namespace std;
const int maxn = 2e5 + 5;
long long a[maxn];
typedef long long ll;
int main()
{
	int n;
	/*cin >> n;*/
	scanf("%d", &n);
	priority_queue<ll, vector<ll>, greater<ll>>que;
	
		for (int i = 0; i < n; i++)
		{
			//cin >> a[i];
			scanf("%d", &a[i]);
			que.push(a[i]);
		}
		if (n == 1)
		{
			cout << 0 << endl;

		}
		else{
			if (n % 2 == 0)que.push(0);

			ll ans = 0;
			while (que.size() > 2)
			{
				ll m1 = que.top();
				que.pop();
				ll m2 = que.top();
				que.pop();
				ll m3 = que.top();
				que.pop();
				ans += m1 + m2 + m3;
				que.push(m1 + m2 + m3);
			}

			cout << ans << endl;
		}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值