The one of the edition distance (II) P48188


Statement
 

pdf   zip

At this stage, you surely already know that some problems are so classic that blah, blah, blah. Nothing new with this problem. Now, we ask you to compute the minimum cost to insert letters into or to modify letters from two words w1w_1 and w2w_2 to make them identical. Both words are made up of only letters chosen among the nn smallest lowercase letters (for instance, for n=4n=4, the alphabet is {a,b,c,d}\{a, b, c, d\}). For every letter (call it xx), inserting an xx in any place in any word has cost IxI_x. The cost to transform a letter xx into a letter yy is given by (Ix+Iy)/4\lceil(I_x+I_y)/4\rceil, i.e., a fourth part, ceiling, of the sum fo the insertion costs IxI_x and IyI_y.

Input

Input consists of several cases. Each case begins with 2n262\leq n\leq 26, followed by nn strictly positive natural numbers $I_{\mbox{\texttt{a}}}, I_{\mbox{\texttt{b}}}, I_{\mbox{\texttt{c}}}, \ldots$. Follow two words w1w_1 and w2w_2 made up of between 1 and 1000 lowercase letters chosen among the nn smallest letters. Assume 1Ix10001\leq I_x\leq 1000 for every letter xx.

Output

For every case, print the minimum cost to make w1w_1 and w2w_2 identical.

Public test cases
  • Input

    2
    11 10
    aaa
    aba
    
    4
    100 100 100 1
    abcd
    bcda
    
    3
    1 10 100
    abbcabccabbac
    bbcabacabbac
    
    4
    1 2 1 4
    dcbbcbbddccdabdbdbdcbbc
    cddcab
    

    Output

    6
    54
    27
    35
    
  • Information
    Author
    Omer Giménez
    Language
    English
    Translator
    Carlos Molina
    Original language
    Spanish
    Other languages
    Spanish
    Official solutions
    C++
    User solutions
    C++