# Codeforces 557C Arthur and Table

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.

Sample test(s)
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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <array>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
using namespace std;
const int maxn = 100010;
vector<int> vec[maxn];
priority_queue<int, vector<int>, less<int> > pq;
int l[maxn], d[maxn], sum[maxn];
vector<int> vv;

int main() {

//freopen("aa.in", "r", stdin);

int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &l[i]);
}
int tot = 0;
int ans = 0x3f3f3f3f;
for(int i = 1; i <= n; ++i) {
scanf("%d", &d[i]);
tot += d[i];
vec[l[i]].push_back(d[i]);
sum[l[i]] += d[i];
}
for(int i = 1; i <= 100000; ++i) {
if(sum[i] == 0) continue;
int t = sum[i];
int s = vec[i].size() - 1;
vv.clear();
int tt = 0;
while(s > 0 && !pq.empty()) {
tt += pq.top();
vv.push_back(pq.top());
pq.pop();
s--;
}
t += tt;
ans = min(ans, tot - t);
for(int j = 0; j < (int)vv.size(); ++j) {
pq.push(vv[j]);
}
for(int j = 0; j < (int)vec[i].size(); ++j) {
pq.push(vec[i][j]);
}
}
printf("%d\n", ans);
return 0;
}

• 本文已收录于以下专栏：

## （CodeForces - 557C）Arthur and Table

（CodeForces - 557C）Arthur and Table Arthur has bought a beautiful big table into his new flat. When ...
• wozaipermanent
• 2017年09月13日 11:08
• 78

## codeforces 557c Arthur and Table

• weishengmingerfendou
• 2015年07月10日 09:23
• 392

## CodeForces 557C Arthur and Table

• YHYYXT
• 2015年07月01日 22:10
• 646

## 【35.29%】【codeforces 557C】Arthur and Table

time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard...
• harlow_cheng
• 2016年12月10日 21:16
• 169

## Problem - 557C - Codeforces - Arthur and Table

Arthur and TableArthur has bought a beautiful big table into his new flat. When he came home, Arthur...
• xiyue_jiang
• 2017年08月06日 10:36
• 54

## 【codeforces 557C Arthur and Table】+ 思维

C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input sta...
• WYK1823376647
• 2016年12月07日 23:24
• 314

## Codeforces 557C Arthur and Table【思维】

C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes inpu...
• mengxiang000000
• 2017年03月04日 11:44
• 527

## 557C - Arthur and Table （前缀和）

• Baoli1008
• 2015年07月26日 13:35
• 419

## 557C Arthur and Table

• zyjhtutu
• 2015年07月21日 10:34
• 260

## Codeforces 557C Arthur and Table 砍桌腿

• FirstLucker
• 2015年07月01日 04:56
• 1629

举报原因： 您举报文章：Codeforces 557C Arthur and Table 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)