Codeforces Round 227 (Div. 2)
E
George and Cards
Codeforces
2200
原题
← D→
翻译中…

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!

翻译中…

Input

翻译中…

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.

翻译中…

Output

翻译中…

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

翻译中…

样例

Input #1
3 2
2 1 3
1 3
Output #1
1
InputCopy OutputCopy
Input #2
10 5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10
Output #2
30
InputCopy OutputCopy
题目信息
题号
387E
时间限制
2 s
内存限制
256 MB
标签
二分
数据结构

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。