Problem:


Score : 200 points

Problem Statement

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.

Sample Input 1

3
3 1 2
2 5 4
3 6

Sample Output 1

14

Takahashi gained 14 satisfaction points in total, as follows:

First, he ate Dish 3 and gained 4 satisfaction points.

Next, he ate Dish 1 and gained 2 satisfaction points.

Lastly, he ate Dish 2 and gained 5 + 3 = 8 satisfaction points.

Sample Input 2

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

Sample Output 2

74

Sample Input 3

2
1 2
50 50
50

Sample Output 3

150



Sample input:
3
3 1 2
2 5 4
3 6


Sample output:
14

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a_list = list(map(int,input().split()))
    b_list = list(map(int,input().split()))
    c_list = list(map(int,input().split()))

    answer = 0
    for i in range(n):
        a = a_list[i]
        answer += b_list[a-1]
        if i < n-1:
            next_a = a_list[i+1]
            if next_a - a == 1:
                answer += c_list[a-1]
    print(answer)

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    before = -1
    satisfied = 0

    for num in A:
        satisfied += B[num-1]
        if (before != -1) and ((before + 1) == num):
            satisfied += C[num-2]
        before = num
    print(satisfied)

Submission status: Accepted

Solution code:
N=int(input())
A=[0]+list(map(int, input().split()))
B=[0]+list(map(int, input().split()))
C=[0]+list(map(int, input().split()))

happy=0
for i in range(1, N+1):
  happy+=B[A[i]]
  
for i in range(1, N):
  if A[i]+1==A[i+1]:
    happy+=C[A[i]]
    
print(int(happy))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
count = B[A[0]-1]
for i in range(1,N):
    count+=B[A[i]-1]
    if A[i] == A[i-1] + 1:
        count+=C[A[i-1]-1]
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=0
tb=-1
for i in range(n):
    t=a[i]
    ans+=b[t-1]
    if t==tb+1:
        ans+=c[tb-1]
    tb=t
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
B.insert(0,0)
C = list(map(int,input().split()))
C.insert(0,0)
cnt = 0
for i in range(N-1):
    cnt += B[A[i]]
    if A[i+1] == A[i]+1:
        cnt += C[A[i]]
cnt += B[A[N-1]]
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 0
for i in range(n):
    ans += b[a[i]-1]
    if i!=n-1 and a[i]+1==a[i+1]:
        ans +=c[a[i]-1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
output = 0
prev = -1
for i in a:
    output += b[i - 1]
    if prev + 1 == i:
        output += c[prev - 1]
    prev = i
print(output)

Submission status: Accepted

Solution code:
N = int(input())
vA = list(map(int, input().split()))
vB = list(map(int, input().split()))
vC = list(map(int, input().split()))

res = 0

p = -100
for a in vA:
    ate = a - 1
    b = vB[ate]
    c = vC[p] if p+1==ate else 0
    res += b + c
    p = ate

print(res)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))

before=n+1
ans=0
for i in range(n):
  now=A[i]
  if now==before+1:
    ans+=B[now-1]+C[before-1]
  else:
    ans+=B[now-1]
  before=now
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
last = -10
for i in range(N):
  eat = A[i] -1 
  ans += B[eat]
  if last == eat - 1:
    ans += C[eat-1]
  
  last = eat
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
prev = 99
for i in a:
    ans += b[i-1]
    if prev == i-1:
        ans += c[prev-1]
    prev = i

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    c = [int(x) for x in input().split()]
    ans = 0
    latter = 60
    for i in range(n):
        now = a[i]-1
        ans += b[now]
        if latter+1 == now:
            ans += c[now-1]
        latter = now
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = 0
for i in range(n):
    ans += b[a[i]-1]
    
for i in range(n-1):
    if a[i+1] - a[i] == 1:
        ans += c[a[i]-1]
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [ int(x)-1 for x in input().split() ]
B = [ int(x) for x in input().split() ]
C = [ int(x) for x in input().split() ]

ans = B[A[0]]
for i in range(1,N):
  ans += B[A[i]]
  if A[i] == A[i-1]+1:
    ans += C[A[i-1]]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

S = sum(B)

for n in range(len(A)-1):
  if A[n]+1==A[n+1]:
    S += C[A[n]-1]
      
print(S)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

total = B[A[0]-1]


for i in range(1, N):
  total += B[A[i]-1]
  if A[i] == A[i-1]+1:
    total += C[A[i-1]-1]
print(total)

Submission status: Accepted

Solution code:
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()))
ans=0
for i in range(N-1):
    ans+=B[A[i]-1]
    if A[i+1] == A[i]+1:
        ans+=C[A[i]-1]
    
ans+=B[A[N-1]-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

satisfy = 0

for i in range(N):
	satisfy += B[A[i] - 1]
	if i != 0 and A[i] == A[i - 1] + 1:
		satisfy += C[A[i - 1] - 1]
		
print(satisfy)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]

ans = 0
mae = -1

for i in range(n):
    ans += b[a[i]-1]
    if a[i] == mae+1:
        ans += c[a[i]-2]
    mae = a[i]
        
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = sum(b) + sum([c[a[i] - 1] for i in range(n - 1) if a[i] + 1 == a[i + 1]])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

t = [0]
for i in range(n-1):
    t.append(a[i]+1)
#print(t)
ans = 0
for i in range(n):
    ans += b[i]
    if a[i] == t[i]:
        ans += c[t[i]-2]
   #     print(c[t[i]-2])
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def solve(N, A, B, C):
    last = -1
    ans = 0
    for a in A:
        ans += B[a-1]
        if a == last+1:
            ans += C[last-1]
        last = a
    return ans


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    ans = solve(N, A, B, C)

    print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))


result = sum(B)
for i in range(N - 1):
    if A[i + 1] == A[i] + 1:
        result += C[A[i] - 1]

print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = sum(b)
for i in range(n-1):
  if a[i]+1 ==a[i+1]:
    ans += c[a[i]-1]
print(ans)

Submission status: Accepted

Solution code:
N  = int(input())
A = list(map(int,input().split()))
B  = list(map(int,input().split()))
C  = list(map(int,input().split()))
ans = 0
tmp = -1
for i in range(N):
    num = A[i]
    ans += B[num - 1]
    if num == tmp + 1:
        ans += C[tmp - 1]
    tmp = num
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))


ans = sum(b)

for i in range(n-1):
    if a[i]+1 == a[i+1]:
        ans +=  c[a[i]-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

last = -1
ans = 0
for i in range(N):
    ans += B[A[i]-1]
    if i==0:
        continue
    if A[i-1]+1 == A[i]:
        ans += C[A[i-1]-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
ans = 0
cur = a[0]
ans += b[cur-1]
for i in range(1,n):
  pre,cur = cur,a[i]
  if cur == pre+1:
    ans += b[cur-1] + c[cur-2]
  else:
    ans += b[cur-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
Cs = list(map(int, input().split()))

sum = 0
for i in range(N):
    sum += Bs[As[i]-1]
    if i != N-1 and As[i+1] == As[i]+1:
        sum += Cs[As[i]-1]

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
ans = sum(B)
for i in range(N-1):
    if A[i] + 1 == A[i+1]:
        ans += C[A[i]-1]
print(ans)

Submission status: Accepted

Solution code:
def calc(N, A, B, C):
    score = sum(B)
    for i in range(N - 1):
        if (A[i] + 1 == A[i + 1]):
            score += C[A[i] - 1]
    return score


N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
C = [int(s) for s in input().split()]
print(calc(N, A, B, C))

Submission status: Accepted

Solution code:
n=int(input())
ass=list(map(int,input().split()))
bs=list(map(int,input().split()))
cs=list(map(int, input().split()))
ans=sum(bs)

for i in range(n-1):
    if ass[i+1]==ass[i]+1:
        ans+=cs[ass[i]-1]

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    c=list(map(int,input().split()))
    m=0
    m+=b[a[0]-1]
    for i in range(1,n):
        m+=b[(a[i]-1)]
        if a[i]==a[i-1]+1:
            m+=c[a[i]-2]

    print(m)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())    
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))

    count = 0
    before = -1
    for i in range(N):
        if before == A[i] - 1:
            count += C[A[i-1]-1]
        count += B[A[i]-1]
        before = A[i]
    print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(x) - 1 for x in input().split()]
    B = [int(x) for x in input().split()]
    C = [int(x) for x in input().split()]
    ans = B[A[0]]
    for n in range(1, N):
        ans += B[A[n]]
        if A[n] - A[n-1] == 1:
            ans += C[A[n-1]]
    print (ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = sum(B)
for i in range(N-1):
    if A[i+1] - A[i] == 1:
        ans += C[A[i]-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0

for i in range(n-1) :
    if a[i] + 1 == a[i+1] :
        ans += c[a[i]-1]

print(ans + sum(b))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
prev = -5

for i in a:
  ans += b[i-1]
  if prev == i-2:
    ans += c[i-2]
  prev = i-1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
sm = sum(b)
for n in range(1,N):
  if a[n] - a[n-1] == 1:
    sm += c[a[n]-2]
print(sm)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
a = readl()
b = readl()
c = readl()

index = -99
count = 0
for i in range(n):
    t = a[i] - 1
    count += b[t]
    if index + 1 == t:
        count += c[index]
    index = t
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

bonus = 0

for i in range(n-1):
    if a[i]+1 == a[i+1]:
        bonus += c[a[i]-1]

print(sum(b)+bonus)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    C = list(map(int, input().split()))
    A = [i-1 for i in A]
    ans = 0
    before = -10
    for a in A:
        ans += B[a]
        if a-1 == before:
            ans += C[a-1]
        before = a
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]", B: "List[int]", C: "List[int]"):
    result = 0
    i_before = -100
    for i in A:
        j = i - 1
        result += B[j]
        if j == i_before + 1:
            result += C[j - 1]
        i_before = j
    print(result)


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    C = [int(next(tokens)) for _ in range(N - 1)]  # type: "List[int]"
    solve(N, A, B, C)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
import queue

mod = 1e9+7

def swap(a,b):
    return (b,a)

def gcd(a,b):
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def divisors(a):
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def main():
    n = int(input())
    a = [x for x in map(int,input().split())]
    b = [x for x in map(int,input().split())]
    c = [x for x in map(int,input().split())]
    eat = n+1
    ans = 0
    for i in range(n):
        ans += b[a[i]-1]
        if a[i]==eat+1:
            ans += c[a[i]-2]
        eat = a[i]
    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
for i in range(n):
    j = a[i]-1
    ans += b[j]
    if i>0:
        if a[i]==a[i-1]+1:
            ans += c[j-1]
print(ans)

Submission status: Accepted

Solution code:
import sys,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

n = onem()

a = []
b = []
c = []

a = l()
b = l()
c = l()
co = 0
for i in range(n):
    co += b[a[i]-1]
    if i >= 1:
        if a[i] == a[i-1]+1:
            co += c[a[i-1]-1]
    
print(co)

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().strip().split()))
b_list = list(map(int, input().strip().split()))
c_list = list(map(int, input().strip().split()))

satisfied = 0

prev = -10
for i in range(N):
	a = a_list[i]
	satisfied += b_list[a - 1]
	if prev + 1 == a:
		satisfied += c_list[prev - 1]
	prev = a

print(satisfied)

Submission status: Accepted

Solution code:
N=int(input())
A= list(map(int, input().split()))
B= list(map(int, input().split()))
C= list(map(int, input().split()))
ans=0
before=10000000
for i in range(N):
    eat=A[i]
    ans+=B[eat-1]
    if before+1==eat:
        ans+=C[before-1]
    before=eat
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ABC140
import sys
#sys.setrecursionlimit(100000)


n=int(input())
tmp = input().split()
a = list(map(lambda a: int(a), tmp))
tmp = input().split()
b = list(map(lambda a: int(a), tmp))
tmp = input().split()
c = list(map(lambda a: int(a), tmp))


ans=0
tmp=0
for i in range(n):
	ans+=b[a[i]-1]
	if(i!=n-1):
		if(a[i]+1==a[i+1]):
			ans+=c[a[i]-1]


print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dishes = [int(x) - 1 for x in input().split()]
scores = [int(x) for x in input().split()]
plusscores = [int(x) for x in input().split()]

result = 0
prev_d = -100
for d in dishes:
  result += scores[d]
  if prev_d + 1 == d:
    result += plusscores[prev_d]
  prev_d = d

print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
pre = 100
ans = 0

for i in a:
    ans += b[i-1]
    if i == pre + 1:
        ans += c[i-2]
    pre = i
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from heapq import heappush, heappop, heapify
from bisect import bisect_left

class Solution:
    def main(self):
        N = int(input())
        A = list(map(lambda x: int(x)-1, input().split()))
        B = list(map(int, input().split()))
        C = list(map(int, input().split()))

        res = 0
        for i in range(N):
            res += B[A[i]]
            if i > 0 and A[i] == A[i-1] + 1:
                res += C[A[i-1]]
        print(res)

if __name__ == '__main__':
    Solution().main()

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))

ans = 0
for i in range(n):
  ans += b[a[i]-1]
  if i==n-1: continue
  if a[i]+1 == a[i+1]:
    ans+=c[a[i]-1]
print(ans)

Submission status: Accepted

Solution code:
#import sys
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
#S = [input() for _ in range(H)]
#T = [list(map(int,input().split())) for _ in range(N)]
pre = -1
man = 0
for i in A:
    man += B[i - 1]
    if pre == i - 1 :
        man += C[pre - 1]
    pre = i
print(man)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans=0
before=-1

for i in a:
	ans+=b[i-1]
	if before+1==i:
		ans+=c[i-2]
	before=i
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + [int(a) for a in input().split()]
B = [0] + [int(a) for a in input().split()]
C = [0] + [int(a) for a in input().split()]

previous = -1
satisfaction = 0

for a in A[1:]:
    satisfaction += B[a]
    if previous == a-1:
        satisfaction += C[a-1]
    previous = a

print(satisfaction)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = 0
back = -2
for i in range(N):
    ans += B[A[i]-1]
    if i >= 1 and back +1 == A[i]:
        ans += C[back-1]
    back = A[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

cnt = 0
pre = -10
for i in range(N):
    a = A[i] - 1
    cnt += B[a]
    if pre+1 == a:
        cnt += C[pre]
    pre = a
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=list(map(int,input().split()))
total=sum(B)
for i in range(N-1):
    if A[i+1]-A[i]==1:
        total+=C[A[i]-1]
print(total)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=[0]+list(map(int,input().split()))
C=[0]+list(map(int,input().split()))
s=B[A[0]]
for i in range(1,n):
  s+=B[A[i]]
  if A[i]==A[i-1]+1:
    s+=C[A[i]-1]
print(s)

Submission status: Accepted

Solution code:
N = int(input())
Ryori = [[int(i) for i in input().split()] for j in range(3)]

ans = 0
before_eat = -1
for i in range(N):
    a = Ryori[0][i] - 1
    b = Ryori[1][a]
    c = Ryori[2][before_eat] if before_eat != -1 and a == before_eat + 1 else 0
    before_eat = a
    ans += b + c
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))

ans = 0
pre_i = 100
for p in range(n):
  i = a[p]
  ans += b[i-1]
  if i >= 2 and pre_i+1 == i:
    ans += c[pre_i-1]
  pre_i = i
print(ans)

Submission status: Accepted

Solution code:
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()))
count = 0
for i in range(n):
  count += b[i]
for j in range(n-1):
  if a[j] + 1 == a[j+1]:
    count += c[a[j]-1]
print(count)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
ans = B[A[0]-1]
cur = A[0]
for i in range(1,n):
    if A[i] == cur+1:
        ans += C[cur-1]
    ans += B[A[i]-1]
    cur = A[i]
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
a_lst = [int(x) for x in stdin.readline().rstrip().split()]
b_lst = [int(x) for x in stdin.readline().rstrip().split()]
c_lst = [int(x) for x in stdin.readline().rstrip().split()]
val = 0
prev = -100

for a in a_lst:
  val += b_lst[a-1]
  if prev + 1 == a:
    val += c_lst[a-2]
  prev = a
 
print(val)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
ans=sum(b)
for i in range(n-1):
    if a[i]+1==a[i+1]:
        ans+=c[a[i]-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(x)-1 for x in input().split()]
B_list = [int(x) for x in input().split()]
C_list = [int(x) for x in input().split()]
ans = 0
prevA = -100
for A in A_list:
    ans += B_list[A]
    if (A == prevA+1):
        ans += C_list[prevA]
    prevA = A
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [int(i) for i in input().split()]
ans = 0
pre = -1
for i in range(n):
    ans += b[a[i] - 1]
    if pre == a[i] - 1:
        ans += c[pre - 1]
    pre = a[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
jun=list(map(int,input().split()))
lis=list(map(int,input().split()))
add=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=lis[i]
for i in range(n-1):
    if jun[i]+1==jun[i+1]:
        ans+=add[jun[i]-1]

print(ans)

Submission status: Accepted

Solution code:
import sys, math

def main():
    N = int( sys.stdin.readline())
    *A, = map(int, sys.stdin.readline().split())
    *B, = map(int, sys.stdin.readline().split())
    *C, = map(int, sys.stdin.readline().split())

    ans = 0
    last = -100
    for a in A:
        ans += B[a-1]
        if a == last + 1:
            ans += C[last -1]
        last = a

    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
inpl = lambda: list(map(int,input().split()))
N = int(input())
A = inpl()
B = inpl()
C = inpl()
prev = -1
ans = sum(B)
for i in range(N):
    if A[i] == prev + 1:
        ans += C[prev-1]
    prev = A[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = sum(B)

for i in range(n-1):
  if A[i+1]-A[i] == 1:
    ans += C[A[i]-1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x:int(x)-1, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
for now, nex in zip(A, A[1:]):
  ans += B[now]
  if now+1 == nex:
    ans += C[now]
ans += B[A[-1]]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

x = sum(B)
for i in range(N-1):
	if A[i] + 1== A[i+1]:
		x += C[A[i]-1]

print(x)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
ans = 0
for i in range(n):
  ans += b[a[i] - 1]
  
for i in range(n-1):
  if a[i+1] == a[i] + 1:
    ans += c[a[i]-1]
  
  

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A,B,C=([int(x) for x in input().split()] for _ in range(3))
A=[x-1 for x in A]
print(sum(B) + sum(C[A[i]] for i in range(N-1) if A[i]+1 == A[i+1]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = 0
last = -1
for a in A:
    ans += B[a-1]
    if last + 1 == a: ans += C[a-2]
    last = a
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
Clist = list(map(int, input().split()))

ans = 0
pre = 100
for i in Alist:
    ans += Blist[i-1]

    if i - pre == 1:
        ans += Clist[pre-1]

    pre = i

print(ans)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

n = ni()
aaa = na()
bbb = na()
ccc = na()
ans = 0
pr = -100
for i in range(n):
    a = aaa[i] - 1
    ans += bbb[a]
    if a == pr + 1:
        ans += ccc[pr]
    pr = a

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

ans = sum(B)
tmp = A[0]
for i in range(1, N):
    if tmp + 1== A[i]:
        ans += C[tmp - 1]
    tmp = A[i]

print (ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

a = 0
for i in range(0,N-1):
    if A[i+1] == A[i] + 1:
        a += C[A[i]-1]

print(sum(B)+a)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
before = -10
result = 0
for i in range(N):
  result += B[A[i] - 1]
  if before + 1 == A[i] - 1:
    result += C[before]
  before = A[i] - 1
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split(' ')]
B = [int(x) for x in input().split(' ')]
C = [int(x) for x in input().split(' ')]

ans = B[A[0]-1]
for i in range(1, N):
    ans += B[A[i]-1]
    if A[i-1] + 1 == A[i]:
        ans += C[A[i-1]-1]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())  
A = np.array(input().split(), dtype=np.int64)
B = np.array(input().split(), dtype=np.int64)
C = np.array(input().split(), dtype=np.int64)

ans = np.sum(B)

for i in range(N-1):
    if A[i+1] - A[i] == 1:
        ans += C[A[i]-1]
        
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
C = [int(x) for x in input().split()]
ans = 0
for i in range(N):
    ans += B[i]
    if i >= 1:
        if A[i] - A[i - 1] == 1:
            ans += C[A[i-1]-1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
ccc = list(map(int, input().split()))

ans = sum(bbb)
for a1, a2 in zip(aaa, aaa[1:]):
    if a1 == a2 - 1:
        ans += ccc[a1 - 1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input().split()[0])
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
c_list = list(map(int, input().split()))

p = 0

for i in range(N):
    b_index = a_list[i] - 1
    p += b_list[b_index]
    if i != 0:
        if a_list[i] - 1 == a_list[i-1]:
            c_index = a_list[i] - 2
            p += c_list[c_index]
print(p)