time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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 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

【题目链接】:http://codeforces.com/contest/557/problem/C

【题解】



可以从最后的答案出发;

先枚举最大的数是多少;

假设它出现的次数为x;

则为了使得他出现的次数大于总的桌腿个数/2;

则最多还需要增加x-1条桌腿才行;

显然要使得x-1条桌腿耗费的总能量最大;

(因为要消耗的能量是砍掉所有桌腿的能量总和减去这2*x-1条桌腿的能量);

这样消耗的能量最小;

同时,比这个桌腿长的桌腿不能出现,所以只能在比这个桌腿短的桌腿中找x-1条桌腿(总和最大);

具体实现:

用计数排序搞;

一开始按照桌腿长度升序排;

然后顺序枚举桌腿i;

    rep1(i,1,n)
{
int l = i,r = i+1,temp=a[i].d;//temp是这2*x-1条桌腿的总能量
while (a[r].l==a[l].l)//是同一种桌腿就累加
{
temp+=a[r].d;
r++;
}
int x = r-l-1;//还要找x-1条桌腿;
//printf("%d\n",x);
rep2(j,200,1)//因为桌腿的能量最大为200所以可以用计数排序,这样满足优先找能量最大的桌腿,且桌腿的长度比该桌腿短.
{
if (x==0) break;//桌腿的数目够了就结束
int s = min(x,bo[j]);
temp+=j*s;//s是这个桌腿的数量,j是它的能量;
x-=s;
}
temp1 = max(temp1,temp);
rep1(j,l,r-1)//搞个前缀和
bo[a[j].d]++;
i = r-1;
}

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 1e5+10;
const int MNUM = 200+10;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); struct abc
{
int l,d;
}; int n,sum=0;
abc a[MAXN];
int bo[MNUM]; bool cmp(abc a,abc b)
{
return a.l < b.l;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
rei(a[i].l);
rep1(i,1,n)
rei(a[i].d),sum+=a[i].d;
sort(a+1,a+1+n,cmp);
int temp1 = 0;
rep1(i,1,n)
{
int l = i,r = i+1,temp=a[i].d;
while (a[r].l==a[l].l)
{
temp+=a[r].d;
r++;
}
int x = r-l-1;
//printf("%d\n",x);
rep2(j,200,1)
{
if (x==0) break;
int s = min(x,bo[j]);
temp+=j*s;
x-=s;
}
temp1 = max(temp1,temp);
rep1(j,l,r-1)
bo[a[j].d]++;
i = r-1;
}
printf("%d\n",sum-temp1);
return 0;
}

【35.29%】【codeforces 557C】Arthur and Table的更多相关文章

  1. JAVA 基础编程练习题29 【程序 29 求矩阵对角线之和】

    29 [程序 29 求矩阵对角线之和] 题目:求一个 3*3 矩阵对角线元素之和 程序分析:利用双重 for 循环控制输入二维数组,再将 a[i][i]累加后输出. package cskaoyan; ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【35.02%】【codeforces 734A】Vladik and flights

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【39.29%】【codeforces 552E】Vanya and Brackets

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. [官方软件] Easy Sysprep v4.3.29.602 【系统封装部署利器】(2016.01.22)--skyfree大神

    [官方软件] Easy Sysprep v4.3.29.602 [系统封装部署利器](2016.01.22) Skyfree 发表于 2016-1-22 13:55:55 https://www.it ...

  8. 【codeforces 29B】Traffic Lights

    [题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. PHP GD 生成图片验证码+session获取储存验证码

    以下分享一个PHP制作的图片验证码案例:案比例如以下图: 运用PHP GD具体请看:http://www.php.net/manual/zh/book.image.php 后台图片代码例如以下: &l ...

  2. 安卓View的缓冲机制

    View组件显示的内容能够通过cache机制保存为bitmap, 主要有下面方法: void  setDrawingCacheEnabled(boolean flag),  Bitmap  getDr ...

  3. affix附加导航插件

    <style> a:focus { outline: none; } .nav-pills { width: 150px; } .nav-pills.affix { top : 10px; ...

  4. HDU 1007 Quoit Design 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...

  5. 前端项目中常用es6知识总结 -- Async、Await让异步美如画

    项目开发中一些常用的es6知识,主要是为以后分享小程序开发.node+koa项目开发以及vueSSR(vue服务端渲染)做个前置铺垫. 项目开发常用es6介绍 1.块级作用域 let const 2. ...

  6. 百度地图API 添加标签

    1.手动创建数据,实际项目则是接受GPS信息 /建立坐标点: // lng:经度 lat:纬度 var points = [ {"lng":112.58,"lat&quo ...

  7. HDU——T 2824 The Euler function

    http://acm.hdu.edu.cn/showproblem.php?pid=2824 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  8. android插件式开发资料整理

    1.DL : Apk动态载入框架 2.android中的动态载入机制

  9. eclipse中导入zico Maven项目

    zico源代码地址:https://github.com/jitlogic/zico 简单的说,git上同步的源代码需要先进行maven编译,然后导入eclipse. 如果没有配置好maven,请参考 ...

  10. vue使用jsonp

    axios不支持jsonp,所以需使用其他插件:vue-jsonp npm i vue-jsonp -S 然后在 src/main.js : import Vue from 'vue' import ...