天天看點

Codeforces Round #311 (Div. 2)-C. Arthur and Table

原題連結

C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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 5legs 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

input

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

output

8
      

把長度為d的桌腿當作最長桌腿,則要砍掉比d大的所有桌腿,假設剩下k個桌腿,長度為d的桌腿有m個,若k >= 2 *m則要利用線段樹找到最小的k - (2 *m - 1)個桌腿,砍掉

#include <bits/stdc++.h>
#define maxn 100005
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
	int l, d;
	int i;
}node[maxn];
int cnt[maxn<<2], sum[maxn<<2];
int d[maxn];
bool cmp1(const Node&a, const Node&b){
	return a.d < b.d;
}
bool cmp2(const Node&a, const Node&b){
	return a.l < b.l;
}
void Update(int n, int e, int l, int r, int k){
	if(l == r){
		cnt[n]++;
		sum[n] += k;
		return ;
	}
	int mid = (l + r) >> 1;
	if(e <= mid)
	 Update(n<<1, e, l, mid, k);
	else
	 Update(n<<1|1, e, mid+1, r, k);
	cnt[n] = cnt[n<<1] + cnt[n<<1|1];
	sum[n] = sum[n<<1] + sum[n<<1|1];
}
int Query(int n, int l, int r, int p){
	if(l == r){
		return sum[n];
	}
	int mid = (l + r) >> 1;
	if(cnt[n<<1] >= p)
	 return Query(n<<1, l, mid, p);
	return sum[n<<1] + Query(n<<1|1, mid+1, r, p - cnt[n<<1]); 
}
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, mins = 1e9;
	
	scanf("%d", &n);
	for(int i = 0; i < n; i++)
	 scanf("%d", &node[i].l);
	for(int i = 0; i < n; i++)
	 scanf("%d", &node[i].d);
	sort(node, node+n, cmp1);
	for(int i = 0; i < n; i++)
	 node[i].i = i;
	sort(node, node+n, cmp2);
	for(int i = n-1; i >= 0; i--)
	 d[i] = d[i+1] +  node[i].d;
	
	int h = 0;
	for(int i = 1; i <= n; i++){
		if(node[i].l != node[i-1].l){
			if(i - h == 1){
				mins = min(mins, d[0] - node[h].d);
			}
			else{
			   if(i - h == 2)
				mins = min(mins, d[0] - node[h].d - node[h+1].d);
				int dd = i - h;
				if(2 * dd > i)
				 mins = min(mins, d[i]);
				else{
				int e = i - (2 * dd - 1);
				int h = Query(1, 0, n-1, e);
				mins = min(mins, h + d[i]);
				}
			}
			for(int j = h; j < i; j++){
				Update(1, node[j].i, 0, n-1,  node[j].d);
			}
			h = i;
		}
	}
	printf("%d\n", mins);
	return 0; 
}