codeforces 578B "Or" Game 位运算 |

You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make  as large as possible, where  denotes the bitwise OR.

Find the maximum possible value of  after performing at most k operations optimally.

Input

The first line contains three integers nk and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8).

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

Output the maximum value of a bitwise OR of sequence elements after performing operations.

Examples

input

Copy

3 1 2
1 1 1

output

Copy

3

input

Copy

4 2 3
1 2 4 8

output

Copy

79

Note

For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is .

For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.

题意:有n个数k次操作 每次操作可以选一个数乘以x(x>=2),求k次操作后数组的或值最大是多少

思路:我们每次操作肯定是 操作在一个可以在更高的位产生1的数  假设这个数是a[p]    首先可以确定没有一个数比a[p]更高位有1

那么 用a[p]    *x,因为  x>=2 所以 又会操作 a[p]*x    *x,所以k次操作作用在一个数上;

那么就枚举那个数,记录前,后缀 或 和就好了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>P;
const int len=2e5+5;
const ll mod=1e9+7;
const double pi=acos(-1.0);
ll a[len];
ll l[len],r[len];
int main()
{	
	ll n,k,x;
	cin>>n>>k>>x;
	ll maxx=-1,p=0;
	for(int i=1;i<=n;++i)
	{
		scanf("%lld",&a[i]);
		l[i]=a[i]|l[i-1];
	}
	for(int i=n;i>=1;--i)
		r[i]=r[i+1]|a[i];
	ll o=1;
	for(int i=1;i<=k;++i)o*=x;
	ll ans=0;
	for(int i=1;i<=n;++i)
		ans=max(ans,l[i-1]|r[i+1]|(a[i]*o));
	cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/86703372
今日推荐