All problems

Problem 445

Minimum ASCII Delete Sum

Medium · 20 pts · String, Dynamic Programming

Problem

Delete characters from two strings so that they become equal, and minimise the total ASCII value of the deleted characters (for example, 'a' is worth 97 and 'b' is worth 98). Unlike counting deletions, the cheapest result is not always the longest common subsequence, because some characters cost more than others. A dynamic program over prefixes, cost[i][j] for the first i characters of A and the first j of B, solves it.

Input

Two lines: strings A and B of lowercase letters.

Output

Print the minimum total ASCII value of deleted characters.

Constraints

1 <= |A|, |B| <= 1000

Example input

sea
eat

Example output

231

Explanation

For 'sea' and 'eat', deleting 's' (115) from 'sea' and 't' (116) from 'eat' leaves 'ea' in both, at a total cost of 231.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Delete Operation for Two Strings Next: Count Square Submatrices with All Ones