#P3253. [JLOI2013] 删除物品

[JLOI2013] 删除物品

Description

Consider the following box redistribution problem:

  1. There are NN items arranged into MM stacks.
  2. All items are identical, but they have different priorities.
  3. You may move only the top item of a stack.
  4. You may move the top item of any stack onto the top of another stack. If this item is currently the highest-priority among all items, you may delete it immediately instead of moving it.
  5. Find the minimum number of moves needed to delete all items. Deletions do not count toward the move count.
  6. This is a simplified version: no two items have the same priority, and M=2M = 2.

Input Format

The first line contains two integers N1N_1, N2N_2 denoting the numbers of items in the two stacks, respectively. Then N1N_1 lines follow, giving the priorities in the first stack from top to bottom; larger numbers indicate higher priority. Then N2N_2 lines follow in the same format for the second stack.

Output Format

For the given testdata, output a single integer, the minimum number of moves.

3 3
1
4
5
2
7
3
6

Hint

Constraints: 1N1+N21000001 \le N_1 + N_2 \le 100000, priorities 107\le 10^7.

Translated by ChatGPT 5