Arthur and Table CodeForces - 557C

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;
}

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88086008