#abc291g. G - OR Sum

G - OR Sum

Score : 600600 points

问题描述

存在长度为 NN 的序列 A=(A0,A1,,AN1)A=(A_0,A_1,\ldots,A_{N-1})B=(B0,B1,,BN1)B=(B_0,B_1,\ldots,B_{N-1})

Takahashi 可以对序列 AA 进行任意次数(包括零次)的以下操作:

  • 对序列 AA 应用左循环移位。换句话说,将 AA 替换为由 Ai=A(i+1)%NA'_i=A_{(i+1)\% N} 定义的新序列,其中 x%Nx\% N 表示 xx 除以 NN 后的余数。

Takahashi 的目标是最大化 i=0N1(AiBi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i),其中 xyx|y 表示 xxyy 的按位逻辑和(按位 OR)。

求最大可能的 i=0N1(AiBi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)

什么是按位逻辑和(bitwise OR)?逻辑和(或 OR 操作)是一种针对两个一比特整数(0011)的操作,定义如下表格所示。按位逻辑和(bitwise OR) 是一种逐比特应用逻辑和的操作。

$x$ $y$ $x|y$
$0$ $0$ $0$
$0$ $1$ $1$
$1$ $0$ $1$
$1$ $1$ $1$

逻辑和在至少一个比特 xxyy11 时产生 11。相反,只有当它们两者都为 00 时才产生 00

示例
0110 | 0101 = 0111```

 `以上为通义千问 qwen-max 翻译,仅供参考。`
## Problem Statement

There are length-$N$ sequences $A=(A_0,A_1,\ldots,A_{N-1})$ and $B=(B_0,B_1,\ldots,B_{N-1})$.  
Takahashi may perform the following operation on $A$ any number of times (possibly zero):

-   apply a left cyclic shift to the sequence $A$. In other words, replace $A$ with $A'$ defined by $A'_i=A_{(i+1)\% N}$, where $x\% N$ denotes the remainder when $x$ is divided by $N$.

Takahashi's objective is to maximize $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)$, where $x|y$ denotes the bitwise logical sum (bitwise OR) of $x$ and $y$.

Find the maximum possible $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)$.

What is the bitwise logical sum (bitwise OR)? The **logical sum** (or the OR operation) is an operation on two one-bit integers ($0$ or $1$) defined by the table below.  
The **bitwise logical sum (bitwise OR)** is an operation of applying the logical sum bitwise.<table><thead><tr align="center"><td align="center">$x$</td><td align="center">$y$</td><td align="center">$x|y$</td></tr></thead><tbody><tr align="center"><td align="center">$0$</td><td align="center">$0$</td><td align="center">$0$</td></tr><tr align="center"><td align="center">$0$</td><td align="center">$1$</td><td align="center">$1$</td></tr><tr align="center"><td align="center">$1$</td><td align="center">$0$</td><td align="center">$1$</td></tr><tr align="center"><td align="center">$1$</td><td align="center">$1$</td><td align="center">$1$</td></tr></tbody></table>

The logical sum yields $1$ if at least one of the bits $x$ and $y$ is $1$. Conversely, it yields $0$ only if both of them are $0$.

##### Example

0110 | 0101 = 0111```

Constraints

  • 2N5×1052 \leq N \leq 5\times 10^5
  • 0Ai,Bi310\leq A_i,B_i \leq 31
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

NN

A0A_0 A1A_1 \ldots AN1A_{N-1}

B0B_0 B1B_1 \ldots BN1B_{N-1}

Output

Print the maximum possible i=0N1(AiBi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i).

Sample Input 1

3
0 1 3
0 2 3

Sample Output 1

8

If Takahashi does not perform the operation, AA remains (0,1,3)(0,1,3), and we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(0|0)+(1|2)+(3|3)=0+3+3=6$;
if he performs the operation once, making A=(1,3,0)A=(1,3,0), we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(1|0)+(3|2)+(0|3)=1+3+3=7$; and
if he performs the operation twice, making A=(3,0,1)A=(3,0,1), we have $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(3|0)+(0|2)+(1|3)=3+2+3=8$.
If he performs the operation three or more times, AA becomes one of the sequences above, so the maximum possible i=0N1(AiBi)\displaystyle\sum_{i=0}^{N-1} (A_i|B_i) is 88, which should be printed.

Sample Input 2

5
1 6 1 4 3
0 6 4 0 1

Sample Output 2

23

The value is maximized if he performs the operation three times, making A=(4,3,1,6,1)A=(4,3,1,6,1),
where $\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(4|0)+(3|6)+(1|4)+(6|0)+(1|1)=4+7+5+6+1=23$.

update @ 2024/3/10 12:09:58