cswork / OAOpen practice workspace

AmazonOA #66

Get Minimum Conflicts (Merge Branches)

Estimated HardJudge-ready

Topics (AI-labelled)String DPPrefix & Ranges

Interleave two strings while keeping the original order of the characters within each string. An inversion is a pair of indices where the earlier character is strictly greater than the later one; find the minimum number of inversions after merging. For the source's zc and qd, the optimum under this definition is 4, not 2.

Input

Two lines of lowercase strings, each of length 1..1000.

Output

Output a single integer answer.

Example 1

Input:

zc
qd

Output:

4

Example 2

Input:

a
b

Output:

0

Example 3

Input:

ba
a

Output:

1

Example notes

Example 1: merging into qdzc gives four inversions (q,d), (q,c), (d,c), (z,c), which is optimal; the source's 2 does not match the all-index-pairs definition. Example 2: merging into ab gives 0. Example 3: merging into aba gives only one inversion, (b,a).

Editorial

Loading solution…