Bacteria——数学分析

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_44056753/article/details/100014546

Recently Monocarp has created his own mini-laboratory!

The laboratory contains n 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 7 merge, one bacterium with size 14 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 n bacteria his laboratory contains into exactly one bacterium.

Input

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

The second line contains n integers a1,a2,…,an (1≤ai≤109), where ai is the size of the i-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 n bacteria his laboratory contains into exactly one bacterium.

Examples

Input

2
1 4

Output

2

Input

3
3 6 9

Output

1

Input

7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Output

1

Note

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

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

思路

通过分析可以知道,将所给序列排序后,从小到大选择两个x,y,(x<y),要满足y%x=0&&y/x为2的n次幂才满足题意,其他的直接输出-1即可,所以这里考虑使用优先队列输入数据,然后在依次判断。

Gym-101911c

#include<iostream>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
priority_queue<ll, vector<ll>, greater<ll> > in;
ll ans;
bool solve()
{
    while(in.size()>=2)
    {
        ll x,y;
        x=in.top();
        in.pop();
        y=in.top();
        in.pop();
        ll num=y/x;
        if(y%x!=0||num&(num-1)!=0)
            return false;
        ans+=log2(num*1.00);
        in.push(y*2);
    }
    return true;
}
int main()
{
    int n;
    cin>>n;
        while(!in.empty())
            in.pop();
        ans=0;
        for(int i=0; i<n; i++)
        {
            ll k;
            scanf("%lld",&k);
            in.push(k);
        }
        if(!solve())
            cout<<"-1\n";
        else
            cout<<ans<<endl;

}

猜你喜欢

转载自blog.csdn.net/weixin_44056753/article/details/100014546