Antipalindrome
시간 제한3초메모리 제한1024 MB
길이 2나 3인 회문 부분 문자열이 없도록, 즉 연속한 세 문자가 모두 다르도록 문자열을 최소 비용으로 바꾼다.
문제
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 to (in alphabetical order) and uppercase letters have numbers from to (also in alphabetical order). For example, symbol has number , symbol has number .
You are given a string consists of the first symbols of alphabet. You are also given a matrix of size , consisting of non-negative integers. The element denotes the cost to change the symbol number to the symbol number at some position of the string . It's guaranteed that .
You should change some symbols of the string in such a way that will not contain any palindrome substring of length more than . Also, find the cheapest way to do that.
Note the changes of the symbol number to the symbol number 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 () --- the size of the alphabet.
The second line contains a string () constisting of only lowercase and uppercase English letters with numbers not greater than .
The -th of the next lines contains integers () --- the cost to change the symbol number to the symbol number .
Note that the length of the string 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 print the single integer --- the minimum cost to obtain such a string. Otherwise print the only integer "-1" (without quotes).