Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.

We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the length of the arrays.

The second line contains n integers ai (0 ≤ ai ≤ 109).

The third line contains n integers bi (0 ≤ bi ≤ 109).

Output

Print a single integer — the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 ≤ l ≤ r ≤ n.

Examples
Input
5
1 2 4 3 2
2 3 3 12 1
Output
22
Input
10
13 2 7 11 8 4 9 8 5 1
5 7 18 9 2 3 0 11 8 6
Output
46
Note

Bitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.

In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.

In the second sample, the maximum value is obtained for l = 1 and r = 9.

题意(虽然题目很水,不过位运算太弱了,还是写了一下):

函数f(a, l, r)表达式为:al | al+1 | al+2 | ...... | ar,其中a为一个集合,ai表示集合a中第i个元素;

现有集合a , b (都有n个元素),求f(a, l, r) + f(b, l , r)的最大值;

思路:首先我想到的就是两个for找l和r,因为数据只有1000,肯定是不会超时的;

不过我对位运算并不熟悉,所以先按样例解析跑了一遍。。

发现2 | 4 | 3 = 1 | 2 | 4 | 3 = 2 | 4 | 3 | 2 = 1 | 2 | 4 | 3 | 2 !!!!很神奇有木有!!!!

由此猜测对任意一个集合的所有子集的所有按位或运算。。最大值等于这个集合本身所有元素间进行位运算。。

样例2也符合这个规律。。。

仔细一想。。不难发现任意两个数进行位或运算后得到的值必然是非减的。。这可以由位或运算的规则证明。。

位或其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就为1。2进制对应位只要有一个为1,其结果就是1.。所有运算结果不可能比原数小。。。

所有这个题目只需要分别把两个集合的元素进行位或运算再相加就可以了啦。。。

代码:

 #include <bits/stdc++.h>
#define ll long long
#define MAXN 100000+10
using namespace std; int main(void)
{
int n, x, y, cnt1=, cnt2=; // 注意cnt1, cnt2赋初值0,如果赋值1的话其结果可能变大
cin >> n;
for(int i=; i<n; i++)
{
cin >> x;
cnt1|=x;
}
for(int j=; j<n; j++)
{
cin >> y;
cnt2|=y;
}
cout << cnt1+cnt2 << endl;
return ;
}

Codeforces Round #344 (Div. 2)(按位或运算)的更多相关文章

  1. Codeforces Round #344 (Div. 2) A

    A. Interview time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  2. Codeforces Round #344 (Div. 2) A. Interview

    //http://codeforces.com/contest/631/problem/Apackage codeforces344; import java.io.BufferedReader; i ...

  3. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  4. Codeforces Round #344 (Div. 2) D. Messenger kmp

    D. Messenger 题目连接: http://www.codeforces.com/contest/631/problem/D Description Each employee of the ...

  5. Codeforces Round #344 (Div. 2) C. Report 其他

    C. Report 题目连接: http://www.codeforces.com/contest/631/problem/C Description Each month Blake gets th ...

  6. Codeforces Round #344 (Div. 2) B. Print Check 水题

    B. Print Check 题目连接: http://www.codeforces.com/contest/631/problem/B Description Kris works in a lar ...

  7. Codeforces Round #344 (Div. 2) A. Interview 水题

    A. Interview 题目连接: http://www.codeforces.com/contest/631/problem/A Description Blake is a CEO of a l ...

  8. Codeforces Round #443 (Div. 2) C 位运算

    C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #344 (Div. 2) B. Print Check

    B. Print Check time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Android内存性能优化(内部资料总结) eoe转载

    刚入门的童鞋肯能都会有一个疑问,Java不是有虚拟机了么,内存会自动化管理,我们就不必要手动的释放资源了,反正系统会给我们完成.其实Java中没有指针的概念,但是指针的使用方式依然存在,一味的依赖系统 ...

  2. leetcode 100. Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. php服务端写日志文件

    1.需求 在服务端记录日志 2.基础版 最基础的文件读写,(要注意window和linux的换行符,window是\r\n,linux是\n),这里就写入一个时间. <?php $handle ...

  4. const 和宏的区别

    参考:http://blog.sina.com.cn/s/blog_79b01f6601018xdg.html (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行 ...

  5. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  6. ubuntu配置apache的虚拟主机

    ubuntu中apache的配置文件分散在几个文件中,/etc/apache2/apache2.conf将它们组织起来.这样设计有很多好处,这里就不在赘述了.进入正题: 1)配置文件在/etc/apa ...

  7. Python PEP8规范

    转载自:http://blog.csdn.net/kellyseeme/article/details/50644893 风格指南:http://zh-google-styleguide.readth ...

  8. Python判断当前用户是否是root

    import osif os.geteuid() != 0:print "This program must be run as root. Aborting."sys.exit( ...

  9. 解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now

    先查看文件真正的属性是什么? [root@xxxxx ~]# tar -zxvf tcl8.4.16-src.tar.gz  gzip: stdin: not in gzip format tar: ...

  10. Java Log4j日志使用

    在程序中使用log4j 1.导入包import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator; 2.获取lo ...