eolymp
bolt
Try our new interface for solving problems
Problems

String potential

String potential

You are given a string \textbf{s} containing \textbf{k} first lower-case letters of Latin alphabet. As in the \href{/problems/2158}{problem}, we define a distance between two symbols \textbf{s_i}, \textbf{s_j} of this string as difference between their positions, i.e. |\textbf{j-i}|. Let us define the potential of string in the following way. Let the function \textbf{f}(\textbf{c_1}, \textbf{c_2}) which for each pair of letters gives some weight is known. This function is symmetric with respect to arguments, i.e. \textbf{f}(\textbf{c_1}, \textbf{c_2}) = \textbf{f}(\textbf{c_2}, \textbf{c_1}). The potential between the pair of symbols \textbf{s_i}, \textbf{s_j} will be defined as a product of a pair weight and a distance between of its symbols. The string potential will be calculated by summing up of potential of all pairs of symbols. Write the program that calculates the potential of given string \textbf{s}. \InputFile The first line of the input file contains integer \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{26}), the number of first Latin letters allowing in string. The second line contains a string \textbf{s}. Its length does not exceed \textbf{10^6}. In following \textbf{k} lines the weight function is given. \textbf{i}-th line consists of \textbf{i} numbers, \textbf{j}-th number in \textbf{i}-th line defines the weight for \textbf{i}-th and \textbf{j}-th Latin letters. All weights do not exceed \textbf{10^6} by absolute value. \OutputFile Output one number -- the potential of a string \textbf{s}.
Time limit 1 second
Memory limit 64 MiB
Input example #1
3
abcacba
1
0 1
1 0 1
Output example #1
32
Source International Collegiate Programming Contest, Ukraine, Quarter-Final,May 19, 2011