cswork / OAOpen practice workspace

AmazonOA #299

Minimum Merge Conflicts

Estimated HardJudge-ready

Topics (AI-labelled)String DPGrid DP

Merge two lowercase strings while keeping the internal relative order of each original string, and minimize the number of pairs i<j in the merged string with s[i]>s[j]. Inversions within the same original string also count; equal characters do not.

Input

The two lines contain primary and secondary; the original bounds are 1≤length≤1000 for both, with lowercase letters only.

Output

Output a single integer, the answer.

Example 1

Input:

zc
d

Output:

2

Example 2

Input:

dae
add

Output:

1

Example 3

Input:

aa
aa

Output:

0

Example notes

The answers are 2, 1 and 0. The second example can be merged into adadde. Counting only cross-string inversions, as the source does, would undercount the first example as 1; the adddae shown in the source is also not an optimal arrangement for the second example.

Editorial

Loading solution…