Enchanted Fortress
시간 제한2초메모리 제한1024 MB
길이 30 이하의 문자열에서 부분집합을 골라, 선택된 두 위치의 가중치 d[i][j] 합이 최대가 되도록 한다.
문제
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 ;
- all symbols should be unique;
- the strength of the spell depends only on which symbols are chosen, but not on their order;
- if symbols and , , both exist in the spell, then its strength increases by .
For instance, if 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 , that will not be empty and may contain only big Latin letters or symbols !, ?, @, *. All symbols will be pairwise different. Its length will thus not exceed 30.
The following lines contain the description of the matrix . The -th of these lines () contains integer numbers , , , , separated by single whitespace symbols. These numbers do not exceed 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.