/
Time Limit: 2 sec / Memory Limit: 1024 MiB
問題文
N 個の鍵盤を持つピアノがあります。左から i 番目の鍵盤を押すと A_i ヘルツの音がでます。
鍵盤は音の順に並んでいます。すなわち、A_1 < A_2 < \dots < A_N を満たします。
あなたはこのピアノを使って右手の人差し指だけで曲を弾こうとしています。
曲は B_1,B_2,\dots,B_M ヘルツの M 個の音を順に並べたものです。
鍵盤 i の次に鍵盤 j を押すには指を距離 |i-j| 動かす必要があるとき、曲の最初の音を弾いてから最後の音を弾くまでに指を動かす距離を求めてください。
制約
- 1 \leq N \leq 3\times 10^5
- 1 \leq M \leq 3\times 10^5
- 1 \leq A_i,B_i \leq 10^9
- 鍵盤は音の順に並んでいる。すなわち、 A_1 < A_2 < \dots < A_N を満たす。
- 曲をピアノで弾くことができる。すなわち、全ての i に対しある k が存在し、A_k=B_i を満たす。
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
出力
答えを出力せよ。
入力例 1
8 7 262 294 330 349 392 440 494 523 262 262 392 392 440 440 392
出力例 1
6
各音を出すための鍵盤の位置は左から 1,1,5,5,6,6,5 番目であるため、指の移動距離は順に 0,4,0,1,0,1 となり、合計 6 です。
入力例 2
9 32 1 2 3 4 5 6 7 8 9 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
出力例 2
92
Problem Statement
There is a piano with N keys. Key i plays an A_i-hertz tone.
The keys are in ascending order; that is, A_1 < A_2 < \dots < A_N.
You want to play a song with this piano using only your right index finger.
The song is a sequence of M tones, each of B_1,B_2,\dots, and B_M hertz, in this order.
Pressing key j after key i requires moving the finger by the distance |i-j|. Find the total distance required for the finger to move since playing the first tone until playing the last.
Constraints
- 1 \leq N \leq 3\times 10^5
- 1 \leq M \leq 3\times 10^5
- 1 \leq A_i,B_i \leq 10^9
- The keys are in ascending order; that is, A_1 < A_2 < \dots < A_N.
- The song can be played by the piano; in other words, for every i there exists k such that A_k=B_i.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N B_1 B_2 \dots B_M
Output
Print the answer.
Sample Input 1
8 7 262 294 330 349 392 440 494 523 262 262 392 392 440 440 392
Sample Output 1
6
You need to press keys 1,1,5,5,6,6, and 5 to play the tones, so the finger's traveling distances are 0,4,0,1,0, and 1 in order, for a total of 6.
Sample Input 2
9 32 1 2 3 4 5 6 7 8 9 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
Sample Output 2
92