Interpersonal dislikes P98089


Statement
 

pdf   zip

thehtml

Given n people and the grade of dislike between them, choose how to make them sit at a long table, in such a way that the sum of the interpersonal dislikes of the neighbor persons is minimum, with one restriction: the leftmost person must be the first person given.

Input

Input consists of several cases, each with n, followed by n different names, followed by an n × n matrix of natural numbers between 0 and 106, where the position (i, j) has the dislike between people i and j. The matrix is symmetric, with zeroes at the diagonal. You can assume 1 ≤ n ≤ 12.

Output

For every case, print the minimum sum of dislikes, followed by the optimum placement of people at the table. The test cases are such that there is always a unique solution.

Public test cases
  • Input

    3
    anna maria nuria
    0 3 1
    3 0 9
    1 9 0
    
    1
    salvador
    0
    
    4
    a b c d
    0 1000 1000000 10
    1000 0 50000 30
    1000000 50000 0 7
    10 30 7 0
    

    Output

    10
    anna nuria maria
    0
    salvador
    1037
    a b d c
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Salvador Roura
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C++
    User solutions
    C++ Python