CodeForces 631A-Interview

一时失言乱红尘 2022-08-20 15:16 139阅读 0赞

A. Interview

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 x**l, x**l + 1, …, x**r, where x**i 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.

63e3e61ff1edf034ce061030cbf99def6f385da3.png

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 a**i (0 ≤ a**i ≤ 109).

The third line contains n integers b**i (0 ≤ b**i ≤ 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

  1. 5
  2. 1 2 4 3 2
  3. 2 3 3 12 1

Output

  1. 22

Input

  1. 10
  2. 13 2 7 11 8 4 9 8 5 1
  3. 5 7 18 9 2 3 0 11 8 6

Output

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

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int wc[1100],wc1[1100];
  6. int xx[1100],xx1[1100];
  7. int sum[1111];
  8. int main()
  9. {
  10. int n;
  11. while(scanf("%d",&n)!=EOF)
  12. {
  13. int i,j;
  14. memset(xx,0,sizeof(xx));
  15. memset(xx1,0,sizeof(xx1));
  16. for(i=0;i<n;i++)
  17. {
  18. scanf("%d",&wc[i]);
  19. if(i==0)
  20. {
  21. xx[i]=wc[0];
  22. }
  23. if(i>=1)
  24. {
  25. xx[i]=max(wc[i-1],xx[i-1]|wc[i]);
  26. //printf("%d\n",xx[i]);
  27. }
  28. }
  29. /*for(i=0;i<n;i++)
  30. {
  31. printf("%d\n",xx[i]);
  32. }*/
  33. for(i=0;i<n;i++)
  34. {
  35. scanf("%d",&wc1[i]);
  36. if(i==0)
  37. {
  38. xx1[i]=wc1[0];
  39. }
  40. if(i>=1)
  41. {
  42. xx1[i]=max(wc1[i-1],xx1[i-1]|wc1[i]);
  43. }
  44. }
  45. int maxx=-1;
  46. for (int i=0;i<n;i++)
  47. {
  48. sum[i]=xx1[i]+xx[i];
  49. if (maxx<sum[i]) maxx=sum[i];
  50. }
  51. printf("%d\n",maxx);
  52. }
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,139人围观)

还没有评论,来说两句吧...

相关阅读