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 ai liters. 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.

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

题解:按值爆搜交上去就满了。。。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,maxv=,inf=-1u>>;
int vis[maxn],cnt[maxn],stp[maxn],n;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void init(){
n=read();
queue<pair<int,int> >Q;
for(int i=;i<=n;i++){
int num=read();Q.push(make_pair(num,));
while(!Q.empty()){
int x=Q.front().first,y=Q.front().second;Q.pop();
if(x>maxv||vis[x]==i)continue;
vis[x]=i;cnt[x]++;stp[x]+=y;
Q.push(make_pair(x<<,y+));
Q.push(make_pair(x>>,y+));
}
}
int mi=inf;
for(int i=;i<=maxv;i++)if(cnt[i]==n&&stp[i]<mi)mi=stp[i];
write(mi);
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}

Codeforces Round #312 (Div. 2) C.Amr and Chemistry的更多相关文章

  1. Codeforces Round #312 (Div. 2) C. Amr and Chemistry 暴力

    C. Amr and Chemistry Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/558/ ...

  2. Codeforces Round #312 (Div. 2)B. Amr and The Large Array 暴力

    B. Amr and The Large Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  3. Codeforces Round #312 (Div. 2) B.Amr and The Large Array

    Amr has got a large array of size n. Amr doesn't like large arrays so he intends to make it smaller. ...

  4. C. Amr and Chemistry(Codeforces Round #312 (Div. 2) 二进制+暴力)

    C. Amr and Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  6. Codeforces Round #312 (Div. 2)

    好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...

  7. B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)

    B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

  9. Codeforces Round #312 (Div. 2) A.Lala Land and Apple Trees

    Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. ...

随机推荐

  1. Linux基础系列—Linux体系结构和Linux内核结构

    /** ****************************************************************************** * @author    暴走的小 ...

  2. HDU 2476 String painter(区间dp)

    题意: 给定两个字符串,让求最少的变化次数从第一个串变到第二个串 思路: 区间dp, 直接考虑两个串的话太困难,就只考虑第二个串,求从空白串变到第二个串的最小次数,dp[i][j] 表示i->j ...

  3. Unix Shell 通配符、转义字符、元字符、特殊字符

    shell通配符: * 匹配0或多个字符 a*b a与b之间可以有任意长度的任意字符, 也可以一个也没有, 如aabcb, a01b, ab等 ? 匹配任意一个字符 a?b a与b之间有且只有一个字符 ...

  4. GridView点击行,选中模版列中CheckBox

    <asp:TemplateField ItemStyle-Width="40px" HeaderText="选择" ItemStyle-Horizonta ...

  5. Android 的 Relative Layout 常量

    android:layout_above 将该控件的底部置于给定ID的控件之上                    --Rule that aligns a child's bottom edge ...

  6. iOS开发之字典数据建立模型步骤

    1. 在控制器属性的(questions)set方法中完成字典转模型的操作 - (NSArray *)questions { if (nil == _questions) { //1.加载plist文 ...

  7. [转]Traits 编程技法+模板偏特化+template参数推导+内嵌型别编程技巧

    STL中,traits编程技法得到了很大的应用,了解这个,才能一窥STL奥妙所在. 先将自己所理解的记录如下: Traits技术可以用来获得一个 类型 的相关信息的. 首先假如有以下一个泛型的迭代器类 ...

  8. work登录页

  9. 《JavaScript dom 编程艺术》 placeholder占位符IE8兼容办法。

    在<JavaScript dom 编程艺术>第11章学来的. 相对于用JavaScript替换文本框的提示语句 <!DOCTYPE html> <html lang=&q ...

  10. 很久之前写的Ajax库

    很久之前写的一个小型AJAX的js,放在上面以免以后想玩了找不到了. // version : 0.1 beta // author : __Ajax function __Ajax(url,opti ...