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

2
1 4

Output

2

Input

3
3 6 9

Output

-1

Input

7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Output

1

题意:给出n个细胞的大小,两个一样大的细胞可以合成一个两倍大小的,也可以增加任意大小任意个细胞,问至少需要多少个细胞可以将所有细胞合成一个,不能合成一个输出-1

题解:先判断每个细胞的原始状态,也就是能分成的最小是多大,若存在两种及以上则不符合条件,若符合,优先队列模拟进行一下即可

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
typedef long long ll;
const int N=2e5+10;
struct node{
	ll x;
	node(){
	}
	node(ll x_):x(x_){
	}
	bool operator <(const node &xx)const
	{
		return x>xx.x;
	}
};
int n;
int main()
{
	ll x;
	while(~scanf("%d",&n))
	{
		priority_queue<node> q;
		node m1,m2;
		ll flag=1,k;
		scanf("%lld",&x);
		q.push(x);
		while(x%2==0)
		{
			x>>=1;
		}
		k=x;
		for(int i=2;i<=n;i++)
		{
			scanf("%lld",&x);
			q.push(x);
			while(x%2==0) x>>=1;
			if(x!=k) flag=0;
		}
		if(flag==0) printf("-1\n");
		else
		{
			ll ans=0;
			while(q.size()>1)
			{
				m1=q.top();q.pop();
				m2=q.top();q.pop();
				if(m1.x!=m2.x)
				{
					q.push(m2);
					ans++;
					q.push(node(m1.x*2));
				}
				else
					q.push(node(m1.x*2));
			}
			printf("%lld\n",ans);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/85052326