E. Maximum Subsequence Value

链接:https://codeforces.ml/contest/1365/problem/E

Ridhiman challenged Ashish to find the maximum valued subsequence of an array aa of size nn consisting of positive integers.

The value of a non-empty subsequence of kk elements of aa is defined as ∑2i∑2i over all integers i≥0i≥0 such that at least max(1,k−2)max(1,k−2) elements of the subsequence have the ii-th bit set in their binary representation (value xx has the ii-th bit set in its binary representation if ⌊x2i⌋mod2⌊x2i⌋mod2 is equal to 11).

Recall that bb is a subsequence of aa, if bb can be obtained by deleting some(possibly zero) elements from aa.

Help Ashish find the maximum value he can get by choosing some subsequence of aa.

Input

The first line of the input consists of a single integer nn (1≤n≤500)(1≤n≤500) — the size of aa.

The next line consists of nn space-separated integers — the elements of the array (1≤ai≤1018)(1≤ai≤1018).

Output

Print a single integer — the maximum value Ashish can get by choosing some subsequence of aa.

Examples

input

Copy

3
2 1 3

output

Copy

3

input

Copy

3
3 1 4

output

Copy

7

input

Copy

1
1

output

Copy

1

input

Copy

4
7 7 1 1

output

Copy

7

Note

For the first test case, Ashish can pick the subsequence {2,3}{2,3} of size 22. The binary representation of 22 is 10 and that of 33 is 11. Since max(k−2,1)max(k−2,1) is equal to 11, the value of the subsequence is 20+2120+21 (both 22 and 33 have 11-st bit set in their binary representation and 33 has 00-th bit set in its binary representation). Note that he could also pick the subsequence {3}{3} or {2,1,3}{2,1,3}.

For the second test case, Ashish can pick the subsequence {3,4}{3,4} with value 77.

For the third test case, Ashish can pick the subsequence {1}{1} with value 11.

For the fourth test case, Ashish can pick the subsequence {7,7}{7,7} with value 77.

代码:

#include
using namespace std;
typedef long long ll;
ll T,n,flag;
ll a[501],k,s;
ll b[501][20],c[501];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    ll max1=0;
    if(n>=3)
    {
        for(int i=i;i<=n-2;i++)
        {
            for(int j=i+1;j<=n-1;j++)
            {
                for(int l=j+1;l<=n;l++)
                {
                    max1=max(max1,a[i]|a[j]|a[l]);
                }
            }
        }
    }
    else if(n==2)
    {
        max1=a[1]|a[2];
    }
    else
    {
        max1=a[1];
    }
    printf("%lld\n",max1);

}

 

你可能感兴趣的:(codeforces,位运算,ACM)