George and Cards

题意翻译

给出一个 $1$ 到 $n$ 的排列(输入中的数组 $p$ ),并给出一个长为 $k$ 的数组 $b$ ,要求从 $p$ 中删去 $n - k$ 个元素后得到数组 $b$ 。 删除操作的定义:选取一个区间 $[l, r]$ ,删去其中最小的元素,并获得 $r - l + 1$ 的分数。 请你输出最终得分的最大值,保证有解。 $1 \leq k \leq n \leq 10^{6}$

题目描述

George is a cat, so he loves playing very much. Vitaly put $ n $ cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from $ 1 $ to $ n $ . Then the $ i $ -th card from the left contains number $ p_{i} $ ( $ 1<=p_{i}<=n $ ). Vitaly wants the row to have exactly $ k $ cards left. He also wants the $ i $ -th card from left to have number $ b_{i} $ written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation $ n-k $ times. In one remove operation George can choose $ w $ ( $ 1<=w $ ; $ w $ is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as $ x_{1},x_{2},...,x_{w} $ (from the left to the right). After that, George can remove the card $ x_{i} $ , such that $ x_{i}<=x_{j} $ for each $ j $ $ (1<=j<=w) $ . After the described operation George gets $ w $ pieces of sausage. George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!

输入输出格式

输入格式


The first line contains integers $ n $ and $ k $ ( $ 1<=k<=n<=10^{6} $ ) — the initial and the final number of cards. The second line contains $ n $ distinct space-separated integers $ p_{1},p_{2},...,p_{n} $ ( $ 1<=p_{i}<=n $ ) — the initial row of cards. The third line contains $ k $ space-separated integers $ b_{1},b_{2},...,b_{k} $ — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for $ n-k $ times.

输出格式


Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

输入输出样例

输入样例 #1

3 2
2 1 3
1 3

输出样例 #1

1

输入样例 #2

10 5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10

输出样例 #2

30