Antipalindrome

아직 제출이 없습니다시간 제한3초메모리 제한1024 MB

문제

Consider an alphabet consisting of lowercase and uppercase English letters. Let's enumerate the letters of the alphabet in such a way that lowercase letters have numbers from 11 to 2626 (in alphabetical order) and uppercase letters have numbers from 2727 to 5252 (also in alphabetical order). For example, symbol bb has number 22, symbol YY has number 5151.

You are given a string ss consists of the first kk symbols of alphabet. You are also given a matrix CC of size k×kk \times k, consisting of non-negative integers. The element C_ijC\_{ij} denotes the cost to change the symbol number ii to the symbol number jj at some position of the string ss. It's guaranteed that C_ii=0C\_{ii} = 0.

You should change some symbols of the string ss in such a way that ss will not contain any palindrome substring of length more than 11. Also, find the cheapest way to do that.

Note the changes of the symbol number ii to the symbol number jj at different positions are counted separately. Note also that you can change the symbol at each position not more than once.

입력

The first line contains one integer kk (1k521 \le k \le 52) --- the size of the alphabet.

The second line contains a string ss (1s51061 \le |s| \le 5 \cdot 10^6) constisting of only lowercase and uppercase English letters with numbers not greater than kk.

The ii-th of the next kk lines contains kk integers C_ijC\_{ij} (0C_ij1090 \le C\_{ij} \le 10^9) --- the cost to change the symbol number ii to the symbol number jj.

Note that the length of the string ss may be quite large, so use fast methods to read it (for example function scanf in C++ language and BufferedReader class in Java language).

출력

If it is possible to get a string without palindrome substrings of length more than 11 print the single integer cc --- the minimum cost to obtain such a string. Otherwise print the only integer "-1" (without quotes).