CF387E.George and Cards

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

George is a cat, so he loves playing very much.

Vitaly put nn 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 11 to nn . Then the ii -th card from the left contains number pip_{i} ( 1<=pi<=n1<=p_{i}<=n ).

Vitaly wants the row to have exactly kk cards left. He also wants the ii -th card from left to have number bib_{i} written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation nkn-k times.

In one remove operation George can choose ww ( 1<=w1<=w ; ww 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 x1,x2,...,xwx_{1},x_{2},...,x_{w} (from the left to the right). After that, George can remove the card xix_{i} , such that xi<=xjx_{i}<=x_{j} for each jj (1<=j<=w)(1<=j<=w) . After the described operation George gets ww 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 nn and kk ( 1<=k<=n<=1061<=k<=n<=10^{6} ) — the initial and the final number of cards.

The second line contains nn distinct space-separated integers p1,p2,...,pnp_{1},p_{2},...,p_{n} ( 1<=pi<=n1<=p_{i}<=n ) — the initial row of cards.

The third line contains kk space-separated integers b1,b2,...,bkb_{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 nkn-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
    
首页