B - Buffet Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200200

問題文

高橋くんは NN 種類の料理が食べ放題のビュッフェに行き、全種類の料理 (料理 11, 料理 22, \ldots, 料理 NN) を 11 度ずつ食べました。

高橋くんが ii (1iN)(1 \leq i \leq N) 番目に食べた料理は料理 AiA_i でした。

高橋くんは、料理 ii (1iN)(1 \leq i \leq N) を食べると満足度 BiB_i を得ます。

また、料理 ii (1iN1)(1 \leq i \leq N - 1) を食べた直後に料理 i+1i+1 を食べると満足度 CiC_i を追加で得ます。

高橋くんが得た満足度の合計を求めてください。

制約

  • 入力は全て整数である。
  • 2N202 \leq N \leq 20
  • 1AiN1 \leq A_i \leq N
  • A1,A2,...,ANA_1, A_2, ..., A_N は全て異なる。
  • 1Bi501 \leq B_i \leq 50
  • 1Ci501 \leq C_i \leq 50

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 ...... ANA_N
B1B_1 B2B_2 ...... BNB_N
C1C_1 C2C_2 ...... CN1C_{N-1}

出力

高橋くんが得た満足度の合計を整数で出力せよ。


入力例 1Copy

Copy
3
3 1 2
2 5 4
3 6

出力例 1Copy

Copy
14

以下のように高橋くんは合計 1414 の満足度を得ました。

  • 高橋くんはまず料理 33 を食べ、満足度 44 を得ました。
  • 高橋くんは次に料理 11 を食べ、満足度 22 を得ました。
  • 高橋くんは最後に料理 22 を食べ、満足度 5+3=85 + 3 = 8 を得ました。

入力例 2Copy

Copy
4
2 3 4 1
13 5 8 24
45 9 15

出力例 2Copy

Copy
74

入力例 3Copy

Copy
2
1 2
50 50
50

出力例 3Copy

Copy
150

Score : 200200 points

Problem Statement

Takahashi went to an all-you-can-eat buffet with NN kinds of dishes and ate all of them (Dish 11, Dish 22, \ldots, Dish NN) once.

The ii-th dish (1iN)(1 \leq i \leq N) he ate was Dish AiA_i.

When he eats Dish ii (1iN)(1 \leq i \leq N), he gains BiB_i satisfaction points.

Additionally, when he eats Dish i+1i+1 just after eating Dish ii (1iN1)(1 \leq i \leq N - 1), he gains CiC_i more satisfaction points.

Find the sum of the satisfaction points he gained.

Constraints

  • All values in input are integers.
  • 2N202 \leq N \leq 20
  • 1AiN1 \leq A_i \leq N
  • A1,A2,...,ANA_1, A_2, ..., A_N are all different.
  • 1Bi501 \leq B_i \leq 50
  • 1Ci501 \leq C_i \leq 50

Input

Input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 ...... ANA_N
B1B_1 B2B_2 ...... BNB_N
C1C_1 C2C_2 ...... CN1C_{N-1}

Output

Print the sum of the satisfaction points Takahashi gained, as an integer.


Sample Input 1Copy

Copy
3
3 1 2
2 5 4
3 6

Sample Output 1Copy

Copy
14

Takahashi gained 1414 satisfaction points in total, as follows:

  • First, he ate Dish 33 and gained 44 satisfaction points.
  • Next, he ate Dish 11 and gained 22 satisfaction points.
  • Lastly, he ate Dish 22 and gained 5+3=85 + 3 = 8 satisfaction points.

Sample Input 2Copy

Copy
4
2 3 4 1
13 5 8 24
45 9 15

Sample Output 2Copy

Copy
74

Sample Input 3Copy

Copy
2
1 2
50 50
50

Sample Output 3Copy

Copy
150


2025-04-14 (Mon)
11:34:13 +00:00