N = int(input()) X = [int(input().replace(" ", "")[::-1], 2) for _ in range(N)] A = [int(i) for i in input().split()] complete = 2 ** N - 1 ans = float("inf") for state in range(2 ** N): cand = sum(a for i, a in enumerate(A) if state >> i & 1) for i, x in enumerate(X): if state & x == x: state |= 1 << i for i, x in enumerate(X): if state & x == x: state |= 1 << i if state != complete: continue ans = min(ans, cand) print(ans)