Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them…
- User
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once.
The i-th dish (1 \leq i \leq N) he ate was Dish A_i.
When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points.
Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points.
Find the sum of the satisfaction points he gained.
Constraints
- All values in input are integers.
- 2 \leq N \leq 20
- 1 \leq A_i \leq N
- A_1, A_2, ..., A_N are all different.
- 1 \leq B_i \leq 50
- 1 \leq C_i \leq 50
Input
Input is given from Standard Input in the following format:
N A_1 A_2 ... A_N B_1 B_2 ... B_N C_1 C_2 ... C_{N-1}
Output
Print the sum of the satisfaction points Takahashi gained, as an integer.
Examples
Input
3 3 1 2 2 5 4 3 6
Output
14
Input
4 2 3 4 1 13 5 8 24 45 9 15
Output
74
Input
2 1 2 50 50 50
Output
150
Assistant
We need to parse the problem: There are N kinds of dishes and Takahashi ate all of them once, but in a specific order given by A_1,...,A_N. So A_i is the dish number eaten at the i-th turn (i from 1 to N). Since he ate each dish exactly once, A is a permutation of 1..N.
When he eats Dish i, he gains B_i satisfaction points. Additionally, when he eats Dish i+1 just after eating Dish i (1 ≤ i ≤ N-1), he gains C_i more satisfaction points.
Wait: The condition "when he eats Dish i+1 just after eating Dish i" seems ambiguous: The "Dish i" and "Dish i+1" refer to dish numbers? Or does it refer to the ordering in the sequence of dishes? Let's read carefully: "When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points. Additionally, when he eats Dish i+1 just after eating Dish i (1 \leq i \leq N - 1), he gains C_i more satisfaction points." This phrasing is ambiguous: It could mean that if the dish number that is eaten next is exactly one greater than the previous dish number, then he gains extra C_i. But the index i used here might refer to the dish numbers, not the position in the sequence. Typically, such problems: "When he eats Dish i, he gains B_i satisfaction. Moreover, if he eats Dish i and then Dish i+1 consecutively, he gains an extra C_i." So the condition is based on dish numbers, not on the order of eating.
But we must verify with examples:
Example 1: N=3 A: 3 1 2 B: 2 5 4 C: 3 6
We need to compute total satisfaction.
If we interpret: Each time he eats dish d, he adds B_d. Also, for each consecutive pair in the sequence (previous dish = A_j, current dish = A_{j+1}), if the previous dish number is i and current dish number is i+1, then add C_i.
Now let's compute: Sequence: 3,1,2. First dish 3: add B_3 = 4. Second dish 1: add B_1 = 2. Check consecutive pair: previous 3, current 1. Since 3 and 1 are not consecutive numbers (3 and 4?), it doesn't match i+1? It would only match if previous = i, current = i+1 for some i. For i=1: previous 1, current 2? That is not here. For i=2: previous 2, current 3? Not here. For i=3: previous 3, current 4? Not. So no extra. Third dish 2: add B_2 = 5. Pair: previous 1, current 2. Now previous=1, current=2. That matches i=1 (since current = i+1 = 2). So add C_1 = 3. Total = 4+2+5+3 = 14. That matches output 14.
But also there is possibility of other interpretation: The condition might be when he eats dish i+1 after dish i in his eating order, i.e., if the positions satisfy? But that seems less likely.
Let's test example 2: N=4 A: 2 3 4 1 B: 13 5 8 24 C: 45 9 15
Compute: Sequence dishes: 2,3,4,1. Add B's: dish2 => 5? Wait B_2 = second element of B list? B list: 13 5 8 24. So B_1=13, B_2=5, B_3=8, B_4=24. But careful: B_i corresponds to dish i. So B_1=13, B_2=5, B_3=8, B_4=24. Thus: dish2 => B_2 =5 dish3 => B_3 =8 dish4 => B_4 =24 dish1 => B_1 =13 Sum base = 5+8+24+13 = 50.
Now extra from consecutive pairs: Pair1: (2,3). previous=2, current=3 -> matches i=2? Actually if previous=i, current=i+1 => i=2 gives previous=2, current=3. So add C_2? C list: C_1=45, C_2=9, C_3=15. So C_2=9. Pair2: (3,4). previous=3, current=4 -> matches i=3? So add C_3=15. Pair3: (4,1). previous=4, current=1 -> not consecutive (4 to 1 is not i,i+1) so no extra. Total extra = 9+15 =24. Total = 50+24 =74. Matches output 74.
Example 3: N=2 A: 1 2 B: 50 50 C: 50
Base: dish1:50, dish2:50 =>100. Pair: (1,2): matches i=1, add C_1=50. Total =150. Matches output.
So interpretation is correct: Extra points when the dish numbers are consecutive: if the dish eaten at position j is i and the dish eaten at position j+1 is i+1, then add C_i. Actually it could also be when the dish number of the next dish is exactly one more than the previous dish's number, regardless of i? But we have C_i indexed by i, so extra is C_{previous dish number}? Or C_{i} where i = previous dish number? In the examples: For pair (2,3) we added C_2, because previous=2. For pair (1,2) we added C_1. For pair (3,4) we added C_3. So indeed, if previous = i and current = i+1, then add C_i.
Thus the problem: Given a permutation A of 1..N, and arrays B[1..N], C[1..N-1], compute sum_{j=1..N} B[A_j] + sum_{j=1}^{N-1} (if A_j+1 == A_{j+1} then C[A_j] else 0).
Constraints: N up to 20, so trivial. Implementation: read N, list A (size N), list B (size N), list C (size N-1). Then compute total = sum(B[a] for a in A) + sum(C[a] for j in range(N-1) if A[j]+1 == A[j+1])? Actually C index is a = A[j] because C_i defined for i=1..N-1, so C[A[j]] only when A[j] <= N-1 (since C only defined up to N-1). But note: if A[j] == N, then there is no i such that i+1 is defined? Actually if previous = N, then current would have to be N+1 to get extra, but N+1 does not exist, so we won't consider because A[j] cannot be N and also A[j+1] = N+1, impossible. So safe.
Thus algorithm: initialize total = 0. For each i from 0 to N-1: total += B[A[i]-1] if we use 0-index, but careful: B is 1-indexed. So we can store B as array of length N+1 (index from 1). Then total += B[A[i]].
Then for j from 0 to N-2: if A[j] + 1 == A[j+1]: total += C[A[j]] (again 1-indexed). Note: C is length N-1, but index A[j] is from 1 to N-1 in such case, because if A[j] == N, then A[j]+1 = N+1 which can't equal A[j+1] (max N). So it's safe.
Print total.
That's it.
But let's confirm with large N. Since N <= 20, no overflow issues.
Edge cases: N=2 minimal. Already example works.
Thus solution is straightforward.
But we should think about possible pitfalls: Input format: N on first line, second line has A_1..A_N separated by spaces, third line B_1..B_N, fourth line C_1..C_{N-1}. All integers.
Implementation in Python:
def main(): import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) # make 1-indexed arrays for convenience B1 = [0] + B # B1[i] for i in 1..N C1 = [0] + C # C1[i] for i in 1..N-1 total = sum(B1[a] for a in A) for i in range(N-1): if A[i] + 1 == A[i+1]: total += C1[A[i]] print(total)
Alternatively, we could directly index B and C with offset: B[a-1] etc.
But careful: C list length N-1; C[a-1] gives C_i for i = a. But a might be up to N-1? For a = N, we wouldn't be in condition, so safe. However if we use C[a-1] for a = N, that would be out-of-range if C length N-1; index N-1? Actually C indices 0..N-2 correspond to i=1..N-1. So a = N => a-1 = N-1, which is out of range (since last index is N-2). So we must ensure we only access when condition holds, and when condition holds a cannot be N because a+1 would be N+1 > N. So it's safe. But to avoid risk, we can use dict or conditional.
Simplify: Use B[a-1] and if condition: total += C[ a-1 ] (since a is previous dish number and a <= N-1). But to be safe, we can use if A[i] < N and A[i]+1 == A[i+1]: total += C[A[i]-1] (since C index shift). That works.
But easier to use 1-indexed arrays by prepending a dummy 0.
Now we should output integer.
That solves.
Thus final answer is implement.
But the instruction says: "Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, ..., Dish N) once." That just says he ate each dish once, but order given by A_i.
Now problem solved.
Now we need to produce final answer: just code.
However the user message says: "You are an AI assistant. Answer in the same language the user wrote their request in." The user wrote in English. So answer in English with code? The typical format for these problems is to provide a solution in Python. Since it's a coding problem, we can output explanation and code.
But the user might just expect the solution. However the chat system expects me to provide a final answer. I'll produce the code in Python.
But sometimes they want the reasoning. The user says: "Takahashi went to an all-you-can-eat buffet..." Possibly the user is asking to solve the problem. So I'll give solution.
Thus final answer:
import sys def main(): input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) # Convert to 1-indexed for easier referencing B = [0] + B C = [0] + C total = sum(B[dish] for dish in A) for i in range(N - 1): if A[i] + 1 == A[i + 1]: total += C[A[i]] print(total) if __name__ == "__main__": main()We should also note that this works within constraints.
Thus final answer. ```python
import sys
def main(): input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split()))
# make 1-indexed arrays for easier index handling B = [0] + B # B[i] for i = 1..N C = [0] + C # C[i] for i = 1..N-1 total = sum(B[dish] for dish in A) for i in range(N - 1): if A[i] + 1 == A[i + 1]: total += C[A[i]] print(total)if name == "main": main()