#P6700. Minimum Transformation Time

    ID: 19908 Type: Default 1000ms 256MiB

Minimum Transformation Time

Minimum Transformation Time

You are given two lowercase strings A and B of equal length n and an integer c. You can perform two types of operations on string A in order to transform it into string B:

  1. Change a single character in A to another character. This operation costs 1 second.
  2. Change all occurrences of a chosen character in A to another character. This operation costs c seconds (applied as a group operation).

For each character L that appears in A, you have two choices:

  • Individual operations: For every position where A[i] = L and B[i] ≠ L, you can change the character individually at a cost of 1 second per change. The total cost for character L is therefore (number of positions with A[i]=L that differ from B[i]).
  • Group operation: Choose a target character R (with R ≠ L) and convert every occurrence of L in A to R in one operation with cost c. After that, for any position where B[i] is not R, you may apply an individual operation to fix that position. Thus, the total group operation cost for letter L is:

    \( c + (M - count_{\{i: B[i] = R\}}) \) seconds, where \(M\) is the total number of occurrences of L in A, and \(count_{\{i: B[i] = R\}}\) is the number of these positions for which B[i] = R. You should choose R to minimize this cost, i.e. maximize \(count_{\{i: B[i] = R\}}\).

Your task is to decide for each distinct letter in A whether to apply a group operation (with an optimally chosen target letter) or individual operations so that the total transformation time is minimized. The answer is the sum of the costs computed for each distinct letter group in A.

Note: There is no interference between operations on different letters in A.

inputFormat

The input consists of three lines:

  1. An integer c representing the cost of the group operation.
  2. A lowercase string A.
  3. A lowercase string B of the same length as A.

outputFormat

Output a single integer — the minimum total time (in seconds) required to transform A into B.

sample

2
abc
bcd
3