C - 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.

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

题意:将某些桌子腿砍掉,使得当前长度最大的桌子腿的数量大于总数量的一半

思路:我们可以枚举所有的桌子腿,找出某一个长度是最适合的,使得代价最小,我们可以想到把大于当前枚举桌子腿的数量给去掉,我们假设我们保留了该长度的桌子腿的数量为x,那么小于该长度的的桌子腿数量最多为x-1。我们可以枚举保留的长度,因为代价最大为200,我们记录下每个代价的个数然后去找

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define MAX 100005 typedef long long ll;
using namespace std;
int a[MAX]; struct node
{
int len,val;
}p[MAX];
bool cmp(node x,node y)
{
return x.len<y.len;
}
int sumlen[MAX],sumcost[MAX];
int vis1[MAX],vis2[MAX];
int summ[205];
int main()
{
int n;
cin>>n;
for(int t=1;t<=n;t++)
{
scanf("%d",&p[t].len);
vis1[p[t].len]++;
}
for(int t=1;t<=n;t++)
{
scanf("%d",&p[t].val);
sumcost[p[t].len]+=p[t].val;
}
sort(p+1,p+n+1,cmp); int maxn1=p[n].len; for(int t=1;t<=maxn1;t++)
{
sumcost[t]+=sumcost[t-1];
} int ans;
int minn=0x3f3f3f3f;
for(int t=1;t<=n;t++)
{
int left=vis1[p[t].len]-1;
ans=sumcost[maxn1]-sumcost[p[t].len];
for(int j=200;j>=1;j--)
{
if(vis2[j]>0)
{
if(left>=vis2[j])
{
left-=vis2[j];
}
else
{
ans+=j*(vis2[j]-left);
left=0;
}
}
}
minn=min(minn,ans);
vis2[p[t].val]++;
} cout<<minn<<endl; return 0;
}

E - Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art.

The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be nested in a matryoshka with a higher number, two matryoshkas can not be directly nested in the same doll, but there may be chain nestings, for example, 1 → 2 → 4 → 5.

In one second, you can perform one of the two following operations:

  • Having a matryoshka a that isn't nested in any other matryoshka and a matryoshka b, such that b doesn't contain any other matryoshka and is not nested in any other matryoshka, you may put a in b;
  • Having a matryoshka a directly contained in matryoshka b, such that b is not nested in any other matryoshka, you may get a out of b.

According to the modern aesthetic norms the matryoshka dolls on display were assembled in a specific configuration, i.e. as several separate chains of nested matryoshkas, but the criminal, following the mysterious plan, took out all the dolls and assembled them into a single large chain (1 → 2 → ... → n). In order to continue the investigation Andrewid needs to know in what minimum time it is possible to perform this action.

Input

The first line contains integers n (1 ≤ n ≤ 105) and k (1 ≤ k ≤ 105) — the number of matryoshkas and matryoshka chains in the initial configuration.

The next k lines contain the descriptions of the chains: the i-th line first contains number mi (1 ≤ mi ≤ n), and then mi numbers ai1, ai2, ..., aimi — the numbers of matryoshkas in the chain (matryoshka ai1 is nested into matryoshka ai2, that is nested into matryoshka ai3, and so on till the matryoshka aimi that isn't nested into any other matryoshka).

It is guaranteed that m1 + m2 + ... + mk = n, the numbers of matryoshkas in all the chains are distinct, in each chain the numbers of matryoshkas follow in the ascending order.

Output

In the single line print the minimum number of seconds needed to assemble one large chain from the initial configuration.

Examples

Input

3 2
2 1 2
1 3

Output

1

Input

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

Output

10

Note

In the first sample test there are two chains: 1 → 2 and 3. In one second you can nest the first chain into the second one and get 1 → 2 → 3.

In the second sample test you need to disassemble all the three chains into individual matryoshkas in 2 + 1 + 1 = 4 seconds and then assemble one big chain in 6 seconds.

这道题应该是这次最简单的签到题,但是我读错了题意,我以为是接的时候可以接一个链,但是却是只能单哥的接

思路:找出1连续的链不拆,其余都拆,一个记录拆的总时间,一个记录拆出来多少链,这样最后连的时候是总链数-1

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define MAX 100005 typedef long long ll;
using namespace std;
int a[MAX]; int main()
{
int n,m;
cin>>n>>m;
int s=0;
int cnt;
int sum=0;
for(int t=1;t<=m;t++)
{
int x;
cin>>x;
cnt=0;
for(int j=1;j<=x;j++)
{
scanf("%d",&a[j]);
}
for(int j=2;j<=x;j++)
{
if(a[j]==j)
{
continue;
}
else
{
s++;
cnt++;
}
}
sum+=cnt+1; }
cout<<s+sum-1<<endl;
return 0;
}

F - Amr and Chemistry

Amr loves Chemistry, and specially doing experiments. He is preparing for a new interesting experiment.

Amr has n different types of chemicals. Each chemical i has an initial volume of ailiters. For this experiment, Amr has to mix all the chemicals together, but all the chemicals volumes must be equal first. So his task is to make all the chemicals volumes equal.

To do this, Amr can do two different kind of operations.

  • Choose some chemical i and double its current volume so the new volume will be 2ai
  • Choose some chemical i and divide its volume by two (integer division) so the new volume will be 

Suppose that each chemical is contained in a vessel of infinite volume. Now Amr wonders what is the minimum number of operations required to make all the chemicals volumes equal?

Input

The first line contains one number n (1 ≤ n ≤ 105), the number of chemicals.

The second line contains n space separated integers ai (1 ≤ ai ≤ 105), representing the initial volume of the i-th chemical in liters.

Output

Output one integer the minimum number of operations required to make all the chemicals volumes equal.

Examples

Input

3
4 8 2

Output

2

Input

3
3 5 6

Output

5

Note

In the first sample test, the optimal solution is to divide the second chemical volume by two, and multiply the third chemical volume by two to make all the volumes equal 4.

In the second sample test, the optimal solution is to divide the first chemical volume by two, and divide the second and the third chemical volumes by two twice to make all the volumes equal 1.

题意比较好理解

题意:对每个数进行*2或/2的操作,使得最后所有的数都是相等的,求最少的操作次数

思路:我们可以枚举可能是从所有的数,分别记录一下把这个数能到达的数及到达需要的次数记录下来,去找的时候

注意奇偶性,偶数可以×2除2操作,奇数只能乘,最后枚举可以到达的数中次数最小的即可

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define MAX 100005 typedef long long ll;
using namespace std;
int a[MAX];
int sum[MAX];
int num[MAX];
int main()
{
int n;
cin>>n;
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
for(int t=1;t<=n;t++)
{
scanf("%d",&a[t]);
}
int maxn=100000;
for(int t=1;t<=n;t++)
{
int k = a[t];
int pre = 0;
while(k)
{
int s = 0;
while(k% 2 == 0)
{
k/= 2;
s++;
}
int y = k;
int x1 = 0;
while(y <= maxn)
{
num[y]++;
sum[y]+=pre+abs(s - x1);
x1++;
y*=2;
}
pre+=s+1;
k/= 2;
}
}
int minn=0x3f3f3f3f; for(int t=1;t<=maxn;t++)
{
if(num[t]==n)
minn=min(minn,sum[t]);
}
cout<<minn<<endl; return 0;
}

QDU_CEF(补)的更多相关文章

  1. Oracle补全日志(Supplemental logging)

    Oracle补全日志(Supplemental logging)特性因其作用的不同可分为以下几种:最小(Minimal),支持所有字段(all),支持主键(primary key),支持唯一键(uni ...

  2. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  3. python 添加tab补全

    在平时查看Python方法用到tab补全还是很方便的. 1. mac 平台 配置如下: mac是类Unix平台,需要在添加一条配置内容到bash_profile 中(默认是没有这个文件,可以新建一个放 ...

  4. 记录一次bug解决过程:else未补全导致数据泄露和代码优化

    一.总结 快捷键ctrl + alt + 四个方向键 --> 倒置屏幕 未补全else逻辑,倒置查询数据泄露 空指针是最容易犯的错误,数据的空指针,可以普遍采用三目运算符来解决 SVN冲突解决关 ...

  5. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  6. 关于用sql语句实现一串数字位数不足在左侧补0的技巧

    在日常使用sql做查询插入操作时,我们通常会用到用sql查询一串编号,这串编号由数字组成.为了统一美观,我们记录编号时,统一指定位数,不足的位数我们在其左侧补0.如编号66,我们指定位数为5,则保存数 ...

  7. jQuery 邮箱下拉列表自动补全

    综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...

  8. 为什么FFT时域补0后,经FFT变换就是频域进行内插?

    应该这样来理解这个问题: 补0后的DFT(FFT是DFT的快速算法),实际上公式并没变,变化的只是频域项(如:补0前FFT计算得到的是m*2*pi/M处的频域值, 而补0后得到的是n*2*pi/N处的 ...

  9. eclipse自动补全的设置

    eclipse自动补全的设置   如果你用过Visual Studio的自动补全功能后,再来用eclipse的自动补全功能,相信大家会有些许失望. 但是eclipse其实是非常强大的,eclipse的 ...

随机推荐

  1. PHP中 null ,false , 区别

    先来测试一下吧: if(0 ==''){ echo '<br/>在PHP中0 ==\'\'' ; } if(0 !==''){ echo '<br/>在PHP中0 !==\'\ ...

  2. rest-framework-----视图

    一:基本视图 写一个出版社的增删改查的resful接口 路由: url(r'^publish/$', views.PublishView.as_view()), url(r'^publish/(?P& ...

  3. madvise、fadvise、posix_madvise和posix_fadvise函数的使用

    系统调用madvise的作用:建议内核如何使用指定段的内存.函数原型如下: #include <sys/mman.h> int madvise(void *addr, size_t len ...

  4. Git教程--廖雪峰

    Git简介 1.Git是目前世界上最先进的分布式版本控制系统(没有之一) 2.集中式和分布式版本控制系统有什么区别呢?      区别在于历史版本维护的位置:Git本地仓库包含代码库还有历史库,在本地 ...

  5. 生日蜡烛——第七届蓝桥杯C语言B组(省赛)第二题

    原创 生日蜡烛 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛. 现在算起来,他一共吹熄了236根蜡烛. 请问,他从多少岁开始过生日party的? 请填写他开始过生日 ...

  6. XE中FMX操作ListBox,添加上千条记录(含图片)

    我之前是想在ListBox的每个Item上添加一个图片,Item上所有的内容都是放在Object里赋值,结果发现加载一百条记录耗时四五秒: procedure TMainForm.AddItem; v ...

  7. laravel中firstOrCreate的使用

    laravel - firstOrCreate(判断是否存在, 不存在则新增数据) 1, 判断goods_name是否存在YKQ003213_G这个参数 2, 不存在则添加数组的内容 3, 需要设置自 ...

  8. C# 调接口

    上一个项目,需要mvc管理后台调接口项目,以便后期的重构扩展,调研后发现后台用的ajax请求,直接调接口可能会有跨域问题,最终在c#代码中实现了这个需求. 1,Ajax请求后台 将接口所需参数传入 2 ...

  9. windows下go编码转换问题

    github上有两个package做编码转换,都是基于iconv,用到了cgo,在linux下没有问题,在windows下用,非常麻烦.采用mingw安装libiconv也不行,一直提示找不到libi ...

  10. 死磕Java之聊聊ThreadLocal源码(基于JDK1.8)

    记得在一次面试中被问到ThreadLocal,答得马马虎虎,所以打算研究一下ThreadLocal的源码 面试官 : 用过ThreadLocal吗? 楼主答 : 用过,当时使用ThreadLocal的 ...