- All Contests
- HourRank 22
- Candy Collection

# Candy Collection

# Candy Collection

Halloween is here! Mancunian runs a candy shop and his friend Liverbird is here to buy candies to give to the children. There are *boxes* of candies in a line. The box contains candies, and the box has color . Liverbird wants to buy all the boxes! But the problem is that he does not have a lot of money. :(

Liverbird will carry all the boxes home using crates. A *crate* will contain a contiguous sequence of candy boxes. (*Note:* Don't confuse boxes with crates; crates will contain boxes and boxes contain candies.) Each box belongs to exactly one crate. Liverbird is also choosy about the boxes in a single crate. He does not want any two boxes in the same crate to have the same color. The cost of a crate is the bitwise OR of the number of candies in the boxes it contains (don't ask Mancunian why). For example, the cost of a crate containing three boxes, containing 1, 2 and 3 candies respectively, is 1 OR 2 OR 3 = 3.

What is the minimum total cost needed to buy all the boxes?

**Input Format**

The first line of input contains , the number of candy boxes.

The second line contains space-separated integers, the of which represents , the color of the box. Colors are represented as positive integers.

The third line contains space-separated integers, the of which represents the number of candies in the box.

**Constraints**

**Subtask**

- For 30% of the maximum points,

**Output Format**

Print a single integer which is the answer to the given problem.

**Sample Input 0**

```
6
5 2 1 3 4 2
1000 0 1000 1 2 3
```

**Sample Output 0**

```
1003
```

**Explanation 0**

Liverbird will use two crates.

The first green crate contains the first three boxes and has cost 1000 OR 0 OR 1000 = 1000.

The second blue crate contains the last three boxes and has cost 1 OR 2 OR 3 = 3.

**Sample Input 1**

```
5
1 2 3 4 1
9 9 9 2 2
```

**Sample Output 1**

```
11
```

**Explanation 1**

Liverbird will use two crates.

The first green crate contains the first, second and third boxes and has cost 9 OR 9 OR 9 = 9.

The second blue crate contains the fourth and the fifth boxes and has cost 2 OR 2 = 2.