Marcin is a coach in his university. There are $n$ students who want to attend a training camp. Marcin is a smart coach, so he wants to send only the students that can work calmly with each other.

Let's focus on the students. They are indexed with integers from $1$ to $n$. Each of them can be described with two integers $a_i$ and $b_i$; $b_i$ is equal to the skill level of the $i$-th student (the higher, the better). Also, there are $60$ known algorithms, which are numbered with integers from $0$ to $59$. If the $i$-th student knows the $j$-th algorithm, then the $j$-th bit ($2^j$) is set in the binary representation of $a_i$. Otherwise, this bit is not set.

Student $x$ thinks that he is better than student $y$ if and only if $x$ knows some algorithm which $y$ doesn't know. Note that two students can think that they are better than each other. A group of students can work together calmly if no student in this group thinks that he is better than everyone else in this group.

Marcin wants to send a group of at least two students which will work together calmly and will have the maximum possible sum of the skill levels. What is this sum?


-----Input-----

The first line contains one integer $n$ ($1 \leq n \leq 7000$) — the number of students interested in the camp.

The second line contains $n$ integers. The $i$-th of them is $a_i$ ($0 \leq a_i < 2^{60}$).

The third line contains $n$ integers. The $i$-th of them is $b_i$ ($1 \leq b_i \leq 10^9$).


-----Output-----

Output one integer which denotes the maximum sum of $b_i$ over the students in a group of students which can work together calmly. If no group of at least two students can work together calmly, print 0.


-----Examples-----
Input
4
3 2 3 6
2 8 5 10

Output
15

Input
3
1 2 3
1 2 3

Output
0

Input
1
0
1

Output
0



-----Note-----

In the first sample test, it's optimal to send the first, the second and the third student to the camp. It's also possible to send only the first and the third student, but they'd have a lower sum of $b_i$.

In the second test, in each group of at least two students someone will always think that he is better than everyone else in the subset.

Difficulty: interview

Starter code:

Solutions:
n=int(input())
lst1=list(map(int,input().split()))[:n]
lst2=list(map(int,input().split()))[:n]
dict={}
for a in lst1:
    if a in dict:
        dict[a]+=1

    else:
        dict[a]=1


ans=0
grp=[]

for k in dict:
    if(dict[k]>1):
        grp.append(k)



for i in range(n):
    for k in grp:
        
        if(lst1[i]|k==k):
            ans +=lst2[i]
            break
print(ans)


# -*- coding: utf-8 -*-
"""
Created on Tue Oct  8 23:02:08 2019

@author: Mridul Garg
"""

n = int(input())

a = [0]*n
b = [0]*n

a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))

for i in range(n):
    a[i] = [a[i], b[i]]
a.sort(reverse = True, key = lambda x: x[0])

same = []
comp = a[0][0]
count = 1
score = a[0][1]
for i in range(1, n):
    if comp == a[i][0] and i == n - 1:
        count += 1
        score += a[i][1]
        same.append([comp, score, i])
    
    elif comp == a[i][0]:
        count += 1
        score += a[i][1]
        
    else:
        if count > 1:
            same.append([comp, score, i-1])
            comp = a[i][0]
            score = a[i][1]
            count = 1
        else:
            comp = a[i][0]
            score = a[i][1]
            count = 1

#scores = []
relevant = [] 
       
if len(same) == 0:
    print(0)
    
else:
    f = same[0][0]
    fscore = same[0][1]
    findex = same[0][2]
    
    for i in range(1, len(same)):
        if f & same[i][0] != same[i][0]:
            relevant.append(same[i][0])
#            fscore += same[i][1]
    
    for i in range(findex + 1, n):
        if f & a[i][0] == a[i][0]:
            fscore += a[i][1]
        else:
            for j in relevant:
                if a[i][0] & j == a[i][0]:
                    fscore += a[i][1]
                    break
                
    print(fscore)
                
    
            
   
#else:
#    
#    for i in same:
#        score = i[1]
#        index = i[2]
#        num = i[0]
#        
#        for j in range(index + 1, n):
#            if num & a[j][0] == a[j][0]:
#                score += a[j][1]
#                
#        scores.append([num, score])
#        
#    print(scores) 
#    temp = scores[0]
#    for i in range(len(scores)):
#        print('============================================')
#        for j in range(len(scores)):
#            if scores[i][0] & scores[j][0] != scores[j][0]:
#                scores[i][1] += scores[j][1]
#                print(scores[i][1])
#        
#    
#    print(scores)
#    print(max(scores))
    




































def bitcode(lis):
    bc = []
    for n in lis:
        if lis[n]>1:
            bc.append(n)
    return bc

n = int(input())
lis = {}
a = list(map(int,input().split()))
b = list(map(int,input().split()))
for bc in a:
    try:
        lis[bc] += 1
    except:
        lis[bc] = 1

bcs = bitcode(lis)
if len(bcs)==0:
    print(0)
else:
    bsum = 0
    for i in range(n):
        if lis[a[i]] > 1:
            bsum += b[i]
            continue
        for bc in bcs:
            if (a[i] & ~bc) == 0:
                bsum += b[i]
                break
    print(bsum)

from collections import defaultdict
import sys


n = int(input())
a = [int(val) for val in input().split()]
b = [int(val) for val in input().split()]

groups = defaultdict(lambda: {'count': 0, 'skill': 0})
for i, val in enumerate(a):
    groups[val]['count'] += 1
    groups[val]['skill'] += b[i]

if len(groups) == n:
    print(0)
    return

group = []
output = 0
individuals = []

for val, desc in list(groups.items()):
    if desc['count'] == 1:
        individuals.append((val, desc['skill']))
    else:
        group.append(val)
        output += desc['skill']

for ival, skill in individuals:
    valid = False
    for gval in group:
        if ival | gval == gval:
            valid = True
    if valid:
        output += skill
print(output)
        


# -*- coding:utf-8 -*-

"""

created by shuangquan.huang at 1/9/20

"""

import collections
import time
import os
import sys
import bisect
import heapq
from typing import List


def issub(u, v):
    if u > v:
        return False
    if u == v:
        return True
    
    b = 1
    for i in range(v.bit_length()+1):
        if v & b < u & b:
            return False
        b <<= 1
    return True


def solve(N, A, B):
    vi = collections.defaultdict(list)
    for i, v in enumerate(A):
        vi[v].append(i)
        
    wc = collections.Counter(A)
    group = {k for k, v in wc.items() if v > 1}
    others = {u for u in wc.keys() if u not in group and any(issub(u, v) for v in group)}
    group |= others
    
    ans = 0
    for u in group:
        ans += sum([B[i] for i in vi[u]])
    
    return ans
    

# def test():
#     N = 7000
#     import random
#     A = [random.randint(1, 10000) for i in range(N)]
#     B = [random.randint(0, 10**9) for _ in range(N)]
#     t0 = time.time()
#     print(solve(N, A, B))
#     print(time.time() - t0)
#
# test()
#
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
print(solve(N, A, B))

n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
lis=[]
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
ans=0        
for i in d:
    if d[i]>1:
        lis.append(i)
for i in range(len(lis)):
    for j in range(n):
        if lis[i]==a[j]:
            ans+=b[j]            
for i in range(n):
    if d[a[i]]==1:
        c=0
        for j in range(len(lis)):
            if a[i]&lis[j]==a[i]:
                lis.append(a[i])
                ans+=b[i]
                break
print(ans)            




n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
lis=[]
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
ans=0        
for i in d:
    if d[i]>1:
        lis.append(i)
for i in range(len(lis)):
    for j in range(n):
        if lis[i]==a[j]:
            ans+=b[j]            
for i in range(n):
    if d[a[i]]==1:
        c=0
        for j in range(len(lis)):
            if a[i]&lis[j]==a[i]:
                lis.append(a[i])
                ans+=b[i]
                break
print(ans)            

import sys
input=sys.stdin.readline
from collections import defaultdict
 
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=defaultdict(int)
for i in range(n):
    c[a[i]]+=1


s=0
for i in range(n):
    for j in c:
        if c[j]>1:
            if j|a[i]==j:
                s+=b[i]
                break
print(s)

from collections import defaultdict
N = int(input())
if(N == 1):
    print(0)
    return
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
Dict = defaultdict(int)
Sum = defaultdict(int)
for i in range(N):
    Dict[a[i]] += 1
    Sum[a[i]] += b[i]
ans = 0
val = 0
Group = []
for i in Dict.keys():
    if(Dict[i]>1):
        Group.append(i)
        ans += Sum[i]
        val |= i
for i in range(N):
    if(Dict[a[i]] == 1):
        for k in Group:
            if(a[i] | k <= k):
                ans += Sum[a[i]]
                break
print(ans)

import sys

input = sys.stdin.readline


def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)


def lcm(a, b):
    return (a * b) / gcd(a, b)


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    d={}
    f={}
    for i in range(n):
        if a[i] not in d:
            d[a[i]]=b[i]
            f[a[i]]=1
        else:
            d[a[i]]+=b[i]
            f[a[i]]+=1
    ans=0
    used=[0]*n
    for i in list(f.keys()):
        if f[i]>1:
            for j in range(n):
                if i|a[j]==i and not used[j]:
                    used[j]=1
    for i in range(n):
        if used[i]:
            ans+=b[i]
    print(ans)
    









    return


def __starting_point():
    main()

__starting_point()