from itertools import product N = int(input()) A = list(map(int, input().split())) B = [list(map(int, input().split())) for _ in range(N)] result = -(10 ** 18) for t in product(range(2), repeat=N): v = 0 for i in range(N): if t[i] == 1: v += A[i] for i in range(N): for j in range(i + 1, N): if t[i] & t[j] == 1: v += B[i][j] if v > result: result = v max_t = t print(result) print(*(i + 1 for i in range(N) if max_t[i] == 1))