Logo
(追記) (追記ここまで)

30562번 - Enchanted Fortress 스페셜 저지다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 1024 MB791268.955%

문제

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 $s$;
  • 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_i$ and $s_j,ドル $i \le j,ドル both exist in the spell, then its strength increases by $d_{i,j}$.

For instance, if $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 $s,ドル that will not be empty and may contain only big Latin letters or symbols !, ?, @, *. All symbols will be pairwise different. Its length $n = |s|$ will thus not exceed 30.

The following $n$ lines contain the description of the matrix $d$. The $i$-th of these lines (1ドル \le i \le n$) contains $n + 1 - i$ integer numbers $d_{i,i},ドル $d_{i,i+1},ドル $\ldots,ドル $d_{i,n},ドル separated by single whitespace symbols. These numbers do not exceed 10ドル^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.

제한

예제 입력 1

ABC
1 -1 2
2 -3
1

예제 출력 1

2
AC

예제 입력 2

@
-1

예제 출력 2

0

예제 입력 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

예제 출력 3

9
FUSRODAH!

힌트

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > The UK & Ireland Programming Contest > UKIEPC 2023 E번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /