Arthur and Table CodeForces - 557C

题目要求计算使得桌子稳定的最小能量消耗。桌子稳定条件是最长腿的数量超过剩余腿数的一半。输入包括初始腿数、各腿长度及锯掉各腿的能量。通过排序腿的长度并选择合适的最长腿数量来最小化总能量。解决方案涉及贪心策略,按腿长排序后,确定最长腿的数量,并优先保留能量消耗大的较短腿。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.

In total the table Arthur bought has n legs, the length of the i-th leg is li.

Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number di — the amount of energy that he spends to remove the i-th leg.

A table with k legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.

Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable.

Input
The first line of the input contains integer n (1 ≤ n ≤ 105) — the initial number of legs in the table Arthur bought.
The second line of the input contains a sequence of n integers li (1 ≤ li ≤ 105), where li is equal to the length of the i-th leg of the table.
The third line of the input contains a sequence of n integers di (1 ≤ di ≤ 200), where di is the number of energy units that Arthur spends on removing the i-th leg off the table.

Output
Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable.

Examples

Input
2
1 5
3 2

Output
2

Input
3
2 4 4
1 1 1

Output
0

Input
6
2 2 1 1 3 3
4 3 5 5 2 1

Output
8

题意:有一个n条腿的桌子,第i个腿长li,锯掉第i个腿花费di,桌子稳定时当且仅当当前桌子剩余的最长腿个数>当前桌子剩余腿总数/2。桌子稳定时的最小花费。

思路:我们设取的腿中腿最长的为x个,那么在取的腿中不是最长的腿有x-1个,因为在保证桌子稳定的情况下锯的腿最少,花费最小。我们对桌子腿长度进行从小到大的排序,当我们取第长度为li的腿当做最长的腿时,所有长度大于li的腿全部都锯掉,然后从长度小于li的的腿中选出x-1个腿保留(肯定保留花费大的,去掉花费小的)。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
struct node{
	int l;
	int val;
}a[100009];
bool cmp(node x,node y)
{
	return x.l<y.l;
}
int num[100009],sumval[100009],sum[100009],have[209];
int main()
{
	int n,ans;
	cin>>n;
	memset(num,0,sizeof(num));
	memset(sum,0,sizeof(sum));
	memset(have,0,sizeof(have));
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].l;
		num[a[i].l]++;
	}
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].val;
		sum[a[i].l]+=a[i].val;
	}
	sort(a+1,a+1+n,cmp);
	sumval[0]=0;ans=inf;
	for(int i=1;i<=100000;i++)
		sumval[i]=sumval[i-1]+sum[i];
	for(int i=1;i<=n;i++)
	{
		int need=num[a[i].l]-1;
		int cost=sumval[100000]-sumval[a[i].l];
		for(int j=200;j>=1;j--)
		{
			if(have[j]>0)
			{
				if(need-have[j]>=0)
					need-=have[j];
				else 
				{
					cost+=j*(have[j]-need);
					need=0;
				}
			}
		}
		have[a[i].val]++;
		ans=min(ans,cost);
	}
	cout<<ans<<endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值