Gym 101911C Bacteria (优先队列)

Recently Monocarp has created his own mini-laboratory!

The laboratory contains nn bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 77 merge, one bacterium with size 1414 is the result.

It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store.

You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Input

The first line contains one integer nn (1≤n≤2⋅105)(1≤n≤2⋅105) — the number of bacteria Monocarp's laboratory contains.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109), where aiai is the size of the ii-th bacterium in the laboratory.

Output

If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1.

Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the nn bacteria his laboratory contains into exactly one bacterium.

Examples

input

Copy

2
1 4

output

Copy

2

input

Copy

3
3 6 9

output

Copy

-1

input

Copy

7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

output

Copy

1

Note

In the first example Monocarp should buy one bacterium having size 11 and one bacterium having size 22. Then Monocarp will have 44bacteria having sizes [1,4,1,2][1,4,1,2]. Then two bacteria having sizes 11 can be merged into one having size 22. Then Monocarp will have 33bacteria having sizes [2,4,2][2,4,2]. Then two bacteria having sizes 22 can be merged into one having size 44. Then Monocarp will have 22 bacteria having sizes [4,4][4,4], which can be merged into one having size 88.

In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria.

In the third example Monocarp needs to buy one bacterium having size 10000000001000000000.

思路:如果最后能合成为一个细菌,那么现在有的细菌一定为2的幂次 ,所以每次拿出两个最小的 kx ,ky(kx<ky),如果ky/kx是整数,并且ky/kx是2的幂次 则ans+=log2(ky/kx),同时 push(ky*2).

代码如下:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define ll long long
using namespace std;
int aa[200005];
priority_queue <ll ,vector<ll> , greater<ll> >q;
int main()
{
    int n;
    scanf("%d",&n);
     while(!q.empty())q.pop();
    for(int i=0;i<n;i++)scanf("%d",&aa[i]),q.push(aa[i]);

    bool tmp=false;
    ll ans=0;
    while(q.size()>=2)
    {
        ll kx=q.top();
        q.pop();
        ll ky=q.top();
        q.pop();
        //cout<<kx<<"----"<<ky<<endl;
        ll k=ky/kx;
        if(ky%kx!=0||k&(k-1)!=0)
        {tmp=true ;break;}
        else
        {
            //cout<<"aaaa"<<endl;
            q.push(ky*2);
            ans+=log2(k*1.00);
            //cout<<ans<<"---";
        }
    }
    if(tmp)cout<<"-1"<<endl;
    else
    {
     cout<<ans<<endl;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值