아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Enchanted Fortress

시간 제한2초메모리 제한1024 MB

요약
길이 30 이하의 문자열에서 부분집합을 골라, 선택된 두 위치의 가중치 d[i][j] 합이 최대가 되도록 한다.
난이도

보통10점 중 7점

유형
완전 탐색, 비트 연산
정답자
아직 제출이 없습니다

문제

Elisabeth the Efficient, a famous magician, was called by Emmanuel the Empowered, the mighty lord of the East Embarkmentlands, to enhance the magical protection of his enchanted fortress.

After arriving at the site and studying the structure of the fortress and the existing spells that protect its integrity, Elisabeth found out the following properties of the spell to design:

  • it should consist of symbols taken from the string ss;
  • all symbols should be unique;
  • the strength of the spell depends only on which symbols are chosen, but not on their order;
  • if symbols s_is\_i and s_js\_j, i≤ji \le j, both exist in the spell, then its strength increases by d_i,jd\_{i,j}.

For instance, if s=s=ABC and

\begin{equation\*} d = \left(\begin{array}{ccc} 1 & -1 & 2 \\\ - & 2 & -3 \\\ - & - & 1 \end{array}\right) \end{equation\*}

then a spell A has strength 1, ABC has strength 2, and AC has strength 4.

However, the problem appeared to be too difficult to Elisabeth, because she has only little experience of working with computers. Can you help her to find the strongest spell?

입력

The first line of the input file contains the string ss, that will not be empty and may contain only big Latin letters or symbols !, ?, @, *. All symbols will be pairwise different. Its length n=∣s∣n = |s| will thus not exceed 30.

The following nn lines contain the description of the matrix dd. The ii-th of these lines (1≤i≤n1 \le i \le n) contains n+1−in + 1 - i integer numbers d_i,id\_{i,i}, d_i,i+1d\_{i,i+1}, …\ldots, d_i,nd\_{i,n}, separated by single whitespace symbols. These numbers do not exceed 10610^6 by the absolute values.

출력

Output the length of the strongest spell in the first line. In the second line, output the spell itself. If there are multiple equally strongest spells, output any of them.

예제3

  1. 예제 1

    입력
    ABC
    1 -1 2
    2 -3
    1
    
    예상 출력
    2
    AC
    
  2. 예제 2

    입력
    @
    -1
    
    예상 출력
    0
    
  3. 예제 3

    입력
    ABDFHORSU!?
    1 -1 1 1 1 1 1 1 1 1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    1 1 1 1 1 1 1 1 -1
    1 1 1 1 1 1 1 -1
    1 1 1 1 1 1 -1
    1 1 1 1 1 -1
    1 1 1 1 -1
    1 1 1 -1
    1 1 -1
    1 -1
    -1
    
    예상 출력
    9
    FUSRODAH!