INF = 1 << 60 N = int(input()) X = [] for _ in range(N): xs = list(map(int, input().split())) b = 0 for j in range(N): if xs[j] == 0: continue b |= 1 << j X.append(b) A = list(map(int, input().split())) def can(mask) -> bool: b = mask update = True while update: update = False for i in range(N): if b & (1 << i): continue if (b & X[i]) == X[i]: b |= 1 << i update = True return (1 << N) - 1 == b ans = INF for i in range(1 << N): if can(i): cost = 0 for j in range(N): if i & (1 << j): cost += A[j] ans = min(ans, cost) print(ans)