Marcin and Training Camp (Codeforces 1210B)
Tags: Greedy, brute force

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.

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.

Sample Inputs:
4
3 2 3 6
2 8 5 10

3
1 2 3
1 2 3

1
0
1

Sample Outputs:
15

0

0

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import math
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = 'x' in file.mode or 'r' not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b'\n') + (not b)
            ptr = self.buffer.tell()
            (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            (self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode('ascii'))
        self.read = lambda : self.buffer.read().decode('ascii')
        self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys
n = int(input())
l = list(map(int, input().split()))
sw = list(map(int, input().split()))
if len(set(l)) == n:
    print(0)
    sys.exit()
d = set()
s = set()
for i in range(n):
    if l[i] in s:
        d.add(l[i])
    s.add(l[i])
ans = 0
for i in range(n):
    for j in d:
        if j | l[i] == j:
            ans += sw[i]
            break
print(ans)