Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

T = input()
t = list(T)
que = t.count("?")

for i in range(que):
  m = t.index("?")
  t[m] = "D"

s = ""  
while t:
  s+= t.pop(0)
  
print(s)

### FIXED CODE ###

T = input()
D = T.replace("?","D")

print(D)
### BUGGY CODE ###

n = int(input())
a = list(input().split(" "))
b = [0 for i in range(n+1)]
for i in range(n):
    b[int(a[i])] += 1
for i in range(n):
    b[int(a[i])] -= 1
    sum = 0
    for j in range(n+1):
        sum += b[j]*(b[j]-1)/2
    print(int(sum))
    b[int(a[i])] += 1

### FIXED CODE ###

n = int(input())
a = list(input().split(" "))
b = [0 for i in range(n+1)]
for i in range(n):
    b[int(a[i])] += 1
sum = 0
for j in range(n+1):
        sum += b[j]*(b[j]-1)/2
for i in range(n):
    print(int(sum)-b[int(a[i])]+1)
### BUGGY CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W, K = lr()
SX, SY, GX, GY = lr()
SX -= 1; SY -= 1; GX -= 1; GY -= 1
pond = [sr() for _ in range(H)]
INF = 10 ** 10
dist = [[INF] * W for _ in range(H)]
dist[SX][SY] = 0
que = deque([(0, SX, SY)])
UDLR = [(0, 1), (1, 0), (0, -1), (-1, 0)]
while que:
    d, px, py = que.popleft()
    if px == GX and py == GY:
        print(d); exit()
    if dist[px][py] < d:
        continue
    nd = d + 1
    for dx, dy in UDLR:
        nx = px + dx; ny = py + dy
        for _ in range(K):
            if not (0 <= nx < H) or not (0 <= ny < W) \
                    or dist[nx][ny] < nd or pond[nx][ny] == '@':
                break
            que.append((nd, nx, ny))
            dist[nx][ny] = nd
            nx += dx; ny += dy

print(-1)
            

### FIXED CODE ###

# coding: utf-8
import sys
from collections import deque

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W, K = lr()
SX, SY, GX, GY = lr()
SX -= 1; SY -= 1; GX -= 1; GY -= 1
pond = [sr() for _ in range(H)]
INF = 10 ** 10
dist = [[INF] * W for _ in range(H)]
dist[SX][SY] = 0
que = deque([(0, SX, SY)])
UDLR = [(0, 1), (1, 0), (0, -1), (-1, 0)]
while que:
    d, px, py = que.popleft()
    if px == GX and py == GY:
        print(d); exit()
    if dist[px][py] < d:
        continue
    nd = d + 1
    for dx, dy in UDLR:
        nx = px + dx; ny = py + dy
        for _ in range(K):
            if not (0 <= nx < H) or not (0 <= ny < W) \
                    or dist[nx][ny] < nd or pond[nx][ny] == '@':
                break
            if dist[nx][ny] > nd:
                que.append((nd, nx, ny))
                dist[nx][ny] = nd
            nx += dx; ny += dy

print(-1)
### BUGGY CODE ###

H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_count=[[0]*W for i in range(H)]
from collections import deque
def answer(st_X,st_Y):#X,Y座標を指定してそこからの距離を考える
   tmp_X,tmp_Y=st_X,st_Y
   que=deque([[tmp_X,tmp_Y]])
   after=[]
   while que:#スタート地点からの距離を数えながら行ったことのない場所を探索する
      tmp_X,tmp_Y=que.popleft()
      nu=l_count[tmp_Y][tmp_X]
      after.append([tmp_X,tmp_Y])
      if tmp_X>0 and l[tmp_Y][tmp_X-1]=="." and [tmp_X-1,tmp_Y] not in after and [tmp_X-1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X-1]=nu+1
         que.append([tmp_X-1,tmp_Y])
      if tmp_Y>0 and l[tmp_Y-1][tmp_X]=="." and [tmp_X,tmp_Y-1] not in after and [tmp_X,tmp_Y-1] not in que:
         l_count[tmp_Y-1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y-1])
      if tmp_X<W-1 and l[tmp_Y][tmp_X+1]=="." and [tmp_X+1,tmp_Y] not in after and [tmp_X+1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X+1]=nu+1
         que.append([tmp_X+1,tmp_Y])
      if tmp_Y<H-1 and l[tmp_Y+1][tmp_X]=="." and [tmp_X,tmp_Y+1] not in after and [tmp_X,tmp_Y+1] not in que:
         l_count[tmp_Y+1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y+1])
ans=0
for i in range(H):
   for j in range(W):
      if l[i][j]==".":
         l_count=[[0]*W for i in range(H)]
         answer(j,i)
         tmp=max(sum(l_count,[]))
         ans=max(ans,tmp)
print(ans)

### FIXED CODE ###

H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
l_count=[[0]*W for i in range(H)]
from collections import deque
def answer(st_X,st_Y):#X,Y座標を指定してそこからの距離を考える
   tmp_X,tmp_Y=st_X,st_Y
   que=deque([[tmp_X,tmp_Y]])
   after=set()
   while que:#スタート地点からの距離を数えながら行ったことのない場所を探索する
      tmp_X,tmp_Y=que.popleft()
      nu=l_count[tmp_Y][tmp_X]
      after.add((tmp_X,tmp_Y))
      if tmp_X>0 and l[tmp_Y][tmp_X-1]=="." and (tmp_X-1,tmp_Y) not in after and [tmp_X-1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X-1]=nu+1
         que.append([tmp_X-1,tmp_Y])
      if tmp_Y>0 and l[tmp_Y-1][tmp_X]=="." and (tmp_X,tmp_Y-1) not in after and [tmp_X,tmp_Y-1] not in que:
         l_count[tmp_Y-1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y-1])
      if tmp_X<W-1 and l[tmp_Y][tmp_X+1]=="." and (tmp_X+1,tmp_Y) not in after and [tmp_X+1,tmp_Y] not in que:
         l_count[tmp_Y][tmp_X+1]=nu+1
         que.append([tmp_X+1,tmp_Y])
      if tmp_Y<H-1 and l[tmp_Y+1][tmp_X]=="." and (tmp_X,tmp_Y+1) not in after and [tmp_X,tmp_Y+1] not in que:
         l_count[tmp_Y+1][tmp_X]=nu+1
         que.append([tmp_X,tmp_Y+1])
ans=0
for i in range(H):
   for j in range(W):
      if l[i][j]==".":
         l_count=[[0]*W for i in range(H)]
         answer(j,i)
         tmp=max(sum(l_count,[]))
         ans=max(ans,tmp)
print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))

max = max(a)
for i in range(max,0,-1):
    b = [x%i for x in a]
    # print(b, i, b.count(0))
    if b.count(0) >= n-1:
        break
print(i)


### FIXED CODE ###

def gcd(x, y):
    while y!=0:
        x, y = y, x % y # x,yの大小が逆でも、入れ替わるので、OK
    return x

n = int(input())
a = list(map(int, input().split()))

if n == 2:
    print(max(a))

elif n>=3:
    l = [-1] * n
    r = [-1] * n
    l[0] = a[0]  #一番左を左に挿入
    r[-1] = a[-1] #一番右を右に挿入
# print(l)
# print(r)

    for i in range(1, n): #左から最大公約数をn-1回計算していく
        l[i] = gcd(l[i-1], a[i]) # l[0]とa[1]の最大公約数をl[1]に挿入⇒
        # l[1]とa[2]の最大公約数をl[2]に挿入。隣同士素があったら、それ以降右側全て1
    for i in range(n-2, -1, -1): #右から最大公約数を計算していく
        r[i] = gcd(r[i+1], a[i]) # r[n-1]とa[n-2]の最大公約数をr[n-2]に挿入

        # print("l",l) # lはn-1桁のlist
        # print("r",r)

    ans = max(r[1], l[-2]) # rの左から2番目とlの右から2番目の大きい方を、ansに挿入.
        # 右端、左端は、最大公約数ではないため。(元々の数列)

    for i in range(1, n-1): #両端を切り取った中で、lは右側に寄せて、rは左側に寄せているので、
        g = gcd(l[i-1], r[i+1]) #
        ans = max(ans, g)
        # print(i, g, ans)
    print(ans)
### BUGGY CODE ###

def main():
    import sys
    sys.setrecursionlimit(10**7)
    import collections
    dic=collections.defaultdict(list)

    N,M=list(map(int,sys.stdin.readline().split()))
    for i in range(M):
        x,y=list(map(int,sys.stdin.readline().split()))
        dic[x].append(y)


    dp=[0]*(N+1)
    def dfs(frm):
        if dp[frm]:
            return dp[frm]

        mx=0
        if frm not in dic.keys():
            return 0

        for val in dic[frm]:
            mx=max(mx,dfs(val)+1)

        dp[frm]=mx
        return mx

    mx=0
    for key in dic.keys():
        mx=max(mx,dfs(key))

    return mx

print(main())


### FIXED CODE ###

def main():
    import sys
    sys.setrecursionlimit(10**7)

    N,M=list(map(int,sys.stdin.readline().split()))
    dic=[[] for _ in range(N)]
    for i in range(M):
        x,y=list(map(int,sys.stdin.readline().split()))
        dic[x-1].append(y-1)


    dp=[0]*(N)
    def dfs(frm):
        if dp[frm]:
            return dp[frm]

        mx=0
        if not dic[frm]:
            return 0

        for val in dic[frm]:
            mx=max(mx,dfs(val)+1)

        dp[frm]=mx
        return mx

    mx=0
    for key in range(N):
        mx=max(mx,dfs(key))

    return mx

print(main())
### BUGGY CODE ###

K,S = [int(i) for i in input().split()]

ans = 0
for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i+j+k == S:
                ans += 1
            elif i+j+k > S:
                continue

print(ans)


### FIXED CODE ###

K,S = [int(i) for i in input().split()]

ans = 0
for i in range(K+1):
    for j in range(K+1):
        k = S-i-j
        if k >= 0 and k <= K:
            ans += 1

print(ans)
### BUGGY CODE ###

from sys import stdin
N=(stdin.readline().rstrip())
ans=0
for i in range(1000000):
    cnt=N.count("BW")
    ans=ans+cnt
    N=N.replace("BW","WB")
    if cnt==0:
        break
print(ans)

### FIXED CODE ###

from sys import stdin
N=(stdin.readline().rstrip())
N=list(N)
cnt=0
ans=0
for i in N:
    if i=="B":
        cnt+=1
    else:
        ans+=cnt
print(ans)
### BUGGY CODE ###

import sys 
sys.setrecursionlimit(10**6)
n, k = map(int, input().split())
edges = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)
    
mod = 10**9 + 7
def P(x,y):
    # x = k - 2
    # y = n - 1
    cnt = 0
    ans = 1
    waru = 1
    while cnt < y:
        ans *= (x-cnt)
        ans %= mod
        cnt += 1
    return ans%mod


ans = 1
def dfs(v,p):
    global ans
    us = edges[v]
    for u in us:
        if u == p: continue
        if len(edges[u]) == 1: ans *= 1
        ans *= P(k-2, len(edges[u])-1)   
        dfs(u,v)

dfs(0,-1)
n0 = len(edges[0])
a = P(k,n0+1)
# print(n0,a)
ans *= a
ans %= mod
print(ans)

### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

from collections import deque
# 木構造を作る
N,K = map(int, input().split())
G = [[] for i in range(N)]
for i in range(N-1):
    # グラフに頂点を追加(距離があるときは,u,vの後に入れる)
    u,v = map(int,input().split())
    G[u-1].append((v-1))
    G[v-1].append((u-1))

# print(G)

used = [False] * N
used[0] = True
mod = 10**9 + 7
cnt = 0
ans = K
def dfs(G, i, v):
    global cnt
    global ans 
    #グラフ中の次のVについて
    used[v] = True
    cnt += 1
    numv = 0
    for next_v in G[v]:
        # すでに見られていたら無視
        if used[next_v]: continue
        numv += 1
        ans %= mod
        if i <= 1:
            ans *= K-i-numv
        else:
            ans *= K - 1 - numv
        # print("v",v,"n{},cnt{},ans{}".format(numv,cnt,ans))
        ans %= mod
        dfs(G,i+1, next_v)



dfs(G,0,0)
print(ans%mod)
### BUGGY CODE ###

n = int(input())
s = input()
count = 0

for i in range(len(s)):
        for j in range(i + 1, len(s)):
            if s[j] != s[i]:
                for k in range(j + 1, len(s)):
                    if s[k] != s[j] and s[k] != s[i] and j - i != k - j:
                        count += 1
print(count)


### FIXED CODE ###

import math

n = int(input())
s = input()
count = 0

rCount = s.count("R")
gCount = s.count("G")
bCount = s.count("B")
ans = rCount * gCount * bCount

for dis in range(1, math.ceil(len(s) / 2)):
    for i in range(dis, len(s) - dis):
        if s[i-dis] != s[i] and s[i-dis] != s[i+dis] and s[i] != s[i+dis]:
            ans -= 1
print(ans)
### BUGGY CODE ###

n = int(input())
arr = list(map(int, input().split()))
count = 0
for i in range(n):
    good = 0
    for j in range(n):
        if j != i and arr[i] % arr[j] == 0:
            break
        elif j != i:
            good += 1
    if good == n-1:
        count += 1
print(count)

### FIXED CODE ###

n = int(input())
arr = list(map(int, input().split()))

dp = [0]*(10**6+1)

for i in sorted(arr):
    dp[i] += 1
    if dp[i] == 1:
        for j in range(2*i, len(dp), i):
            dp[j] += 2
print(dp.count(1))
### BUGGY CODE ###

s=input()
t=input()
S=[0 for i in range(26)]
T=[0 for i in range(26)]
for i in range(26):
    for j in range(len(s)):
        if s[j]==chr(i+97):
            S[i]=int(str(S[i])+str(j))
for i in range(26):
    for j in range(len(t)):
        if t[j]==chr(i+97):
            T[i]=int(str(T[i])+str(j))  
ss=sorted(S)
tt=sorted(T)
if ss==tt:
    print('Yes')
else:print('No')

### FIXED CODE ###

#アルファベットの1対1置換が存在すればYes
S=input()
T=input()
#dは、アルファベットの置換、置換は辞書で書く
d={}
#Sを順にみていって、d内になかったらSのi番目とdのi番目が置換関係にある
for i in range(len(S)):
    if S[i] not in d:
        d[S[i]]=T[i] 
    else:
      #1対1の置換でないと判明したらNoを出力
        if d[S[i]]!=T[i]:
            print('No')
            exit()
d={}
for i in range(len(T)):
    if T[i] not in d:
        d[T[i]]=S[i]
    else:
        if d[T[i]]!=S[i]:
            print('No')
            exit()
print('Yes')
### BUGGY CODE ###

S = input()
Q = int(input())
rev = 0

for _ in range(Q):
  a = list(input().split())
  if a[0] == "1":
    rev += 1
  else:
    if rev%2 == 0:
      if a[1] == "1":
        S = a[2] + S
      else:
        S = S + a[2]
    else:
      if a[1] == "2":
        S = a[2] + S
      else:
        S = S + a[2]
      
if rev%2 == 1:
  S = S[::-1]
print(S)

### FIXED CODE ###

S = input()
Q = int(input())
rev = 0
top = []
bot = []

for _ in range(Q):
  a = list(input().split())
  if a[0] == "1":
    rev += 1
  else:
    if rev%2 == 0:
      if a[1] == "1":
        top.append(a[2])
      else:
        bot.append(a[2])
    else:
      if a[1] == "2":
        top.append(a[2])
      else:
        bot.append(a[2])
      
t = "".join(top)[::-1]
b = "".join(bot)
S = "".join([t,S,b])
if rev%2 == 1:
  S = S[::-1]
print(S)
### BUGGY CODE ###

K, S = list(map(int, input().split()))

ans = 0

for x in range(K+1):
  for y in range(K+1):
    for z in range(K+1):
      if x+y+z==S:
        ans += 1
        
print(ans)

### FIXED CODE ###

K, S = list(map(int, input().split()))

ans = 0

for x in range(K+1):
  for y in range(K+1):
    if 0 <= S-x-y <= K:
        ans += 1
        
print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
for i in range(1,n+1):
    print(a.count(i))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))

num = [0] * n

for i in a:
    num[i-1] += 1

print(*num)
### BUGGY CODE ###

n=int(input())
s=0
while n>0:
  r=n%10
  s=s+r
  n=n//10
if s%9==0:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

n=input()
u=sum(map(int, str(n)))
if u%9==0:
  print('Yes')
else:
  print('No')
### BUGGY CODE ###

n=int(input())
h=list(map(int,input().split()))
ans=0
while any(h)==True:
    flag=0
    for i in range(n-1):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
            else:
                if flag==1:
                    ans+=1

print(ans)


### FIXED CODE ###

n=int(input())
h=list(map(int,input().split()))
ans=0
if n==1:
    print(h[0])
    exit()
while any(h)==True:
    flag=0
    for i in range(n-1):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
            else:
                if flag==1:
                    ans+=1
print(ans)
### BUGGY CODE ###

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
INF = 10**12

def binary_search(query, arr):
    arr = [-INF] + arr + [INF]
    low = 0
    high = len(arr)
    t = (low+high)//2
    while low<high-1:
        if query>arr[t]:
            low = t
        else:
            high = t
        t = (low+high)//2
    return arr[low], arr[high]

def find(query):
    s1, s2 = binary_search(query, S)
    t1, t2 = binary_search(query, T)
    return s1, s2, t1, t2

def main():
    for x in X:
        s1, s2, t1, t2 = find(x)
        cands = [
            abs(t1-x) + abs(s2-t1),
            abs(s1-x) + abs(t2-s1),
            abs(s2-x) + abs(t1-s2),
            abs(t2-x) + abs(s1-t2),
            abs(t1-x) + abs(s1-t1),
            abs(s1-x) + abs(t1-s1),
            abs(s2-x) + abs(t2-s2),
            abs(t2-x) + abs(s2-t2)
        ]
        print(min(cands))
    return

main()

### FIXED CODE ###

A, B, Q = map(int, input().split())
INF = 10**12
S = [-INF] + [int(input()) for _ in range(A)] + [INF]
T = [-INF] + [int(input()) for _ in range(B)] + [INF]
X = [int(input()) for _ in range(Q)]


def binary_search(query, arr):
    low = 0
    high = len(arr)
    while high-low>1:
        mid = (low+high)//2
        if query>arr[mid]:
            low = mid
        else:
            high = mid
    return arr[low], arr[high]

def main():
    for x in X:
        s1, s2 = binary_search(x, S)
        t1, t2 = binary_search(x, T)
        ans = [ abs(t-s)+min(abs(t-x), abs(s-x)) \
            for t in [t1, t2] for s in [s1, s2] ]
        print(min(ans))
    return

main()
### BUGGY CODE ###

a,b,k = map(int,input().split())
li,lia = [],[]
for i in range(a,b+1):
    li.append(i)
if k*2 >= b-a+1:
    for i in range(len(li)):
        print(li[i])
    exit()
else:
    for i in range(k):
        lia.append(li[i])
        lia.append(li[-i-1])
    lia.sort()
    for i in range(len(lia)):
        print(lia[i])
    exit()

### FIXED CODE ###

a,b,k = map(int,input().split())
li = []
if k*2 >= b-a+1:
    for i in range(a,b+1):
        li.append(i)
    for i in range(len(li)):
        print(li[i])
else:
    for i in range(k):
        li.append(a+i)
        li.append(b-i)
    li.sort()
    for i in range(len(li)):
        print(li[i])
### BUGGY CODE ###

n,m,r=map(int,input().split())
r=list(map(int,input().split()))

import sys
INF=float('inf')

road=[[INF]*n for _ in range(n)]

for _ in range(m):
    a,b,c=map(int,input().split())
    road[a-1][b-1]=c
    road[b-1][a-1]=c

#経由地
for k in range(n):
    #出発地
    for s in range(n):
        #goal
        for g in range(n):
            road[s][g]=min(road[s][g],road[s][k]+road[k][g])

import itertools
rlist=list(itertools.permutations(r))

result=10**9
for item in rlist:
    tmp=0
    from_town=item[0]
    for to_town in item[1:]:
        tmp+=road[from_town-1][to_town-1]
        from_town=to_town
    result=min(result,tmp)

print(result)





### FIXED CODE ###

n,m,r=map(int,input().split())
r=list(map(int,input().split()))

import sys
INF=float('inf')

road=[[INF]*n for _ in range(n)]

for _ in range(m):
    a,b,c=map(int,input().split())
    road[a-1][b-1]=c
    road[b-1][a-1]=c

#経由地
for k in range(n):
    #出発地
    for s in range(n):
        #goal
        for g in range(n):
            road[s][g]=min(road[s][g],road[s][k]+road[k][g])

import itertools
rlist=list(itertools.permutations(r))

result=10**9
for item in rlist:
    tmp=0
    from_town=item[0]
    for to_town in item[1:]:
        tmp+=road[from_town-1][to_town-1]
        from_town=to_town
    result=min(result,tmp)

print(result)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
ans = 0
prime = 10 ** 9 + 7
max_digit = len(bin(max(A))[2:])
A_bin = list(map(lambda x: bin(x)[2:].zfill(max_digit), A))
zero = [0] * max_digit
one = [0] * max_digit

for num in A_bin:
    for digit in range(max_digit):
        if num[-(digit + 1)] == '0':
            zero[digit] += 1
        else:
            one[digit] += 1

for digit in range(max_digit):
    ans += ((zero[digit] * one[digit]) % prime) * pow(2, digit, prime)
    ans %= prime

print(ans)


### FIXED CODE ###

import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
ans = 0
MOD = 10 ** 9 + 7

for digit in range(60):
    one = np.count_nonzero((A >> digit) & 1)
    ans += (one * (N - one) * pow(2, digit, MOD)) % MOD
    ans %= MOD

print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

loop = [0] * N
cnts = [0] * N
for i in range(N):
  nxt = P[i]-1
  tmp = 0
  for j in range(N):
    tmp += C[nxt]
    nxt = P[nxt]-1
    if nxt == i:
      tmp += C[nxt]
      cnt = j+2
      break
  loop[i] = tmp
  cnts[i] = cnt

mx = [0] * N
for i in range(N):
  if loop[i] > 0:
    mx[i] += ((K//cnts[i])-1) * loop[i]
    
for i in range(N):
  nxt = P[i]-1
  tmp = 0
  m = min(C) - 1
  for j in range((K%cnts[i])+cnts[i]):
    tmp += C[nxt]
    m = max(m, tmp)
    nxt = P[nxt]-1
  mx[i] += m

ans = max(mx)
print(ans)

### FIXED CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))
C = list(map(int, input().split()))

mx = [0] * N
cnts = [0] * N
for i in range(N):
  if mx[i] == 0:
    nxt = P[i]-1
    tmp = 0
    l = [nxt]
    for j in range(N):
      tmp += C[nxt]
      nxt = P[nxt]-1
      l.append(nxt)
      if nxt == i:
        tmp += C[nxt]
        cnt = j+2
        break
    for k in l:
      cnts[k] = cnt
      n = ((K//cnt)-1) * tmp
      if tmp > 0:
        mx[k] += n
    
for i in range(N):
  nxt = P[i]-1
  tmp = 0
  m = min(C) - 1
  for j in range((K%cnts[i])+cnts[i]):
    tmp += C[nxt]
    m = max(m, tmp)
    nxt = P[nxt]-1
  mx[i] += m

ans = max(mx)
print(ans)
### BUGGY CODE ###

N= int(input())

# queryを投げて空の時の処理をする関数
def query(i):
  print(i)
  tmp = input()
  if tmp[0] == 'V':
    exit()
  else:
    return r

# 最初に始点を探索
z = query(0)
  
# 矛盾していないか判定する関数, Falseなら矛盾
def judge(m):
  s = query(m)
  if m % 2 == 0: # 偶数なら0番目と同じなら矛盾しない
    return z == s
  else:
    return z != s
  
# 二分探索する
l = 0
r = N # N-1にするとN-1が探索されずにwhileを抜けてしまう
while r - l > 1:
  m = (l + r) // 2
  if judge(m):
    # l~mまでが矛盾しない
    l = m
  else:
    r = m

### FIXED CODE ###

N= int(input())

# queryを投げて空の時の処理をする関数
def query1(i):
  print(i)
  tmp = input()
  if tmp[0] == 'V':
    exit()
  return tmp

# 最初に始点を探索
z = query1(0)
  
# 矛盾していないか判定する関数, Falseなら矛盾
def judge1(m):
  s = query1(m)
  if m % 2 == 0: # 偶数なら0番目と同じなら矛盾しない
    return z==s
  else:
    return z!=s
  
# 二分探索する
l = 0
r = N # N-1にするとN-1が探索されずにwhileを抜けてしまう
while r - l > 1:
  m = (l + r) // 2
  if judge1(m):
    # l~mまでが矛盾しない
    l = m
  else:
    r = m
### BUGGY CODE ###

from collections import deque
H, W = [int(i) for i in input().split()]
Shw = [[0 if i =="." else 1 for i in input()] for _ in range(H)]

changes = [[-1 for _ in range(W)] for _ in range(H)]
que = deque()
i0 = 0
j0 = 0
que.append((i0, j0,  0, 0))

while len(que) > 0:
  i, j,  o, c = que.popleft()
  current = Shw[j][i]
  diff = 1 if current > 0 and o == 0 else 0
  change = c + diff
  if changes[j][i] == -1:
    changes[j][i] = change
  else:
    changes[j][i] = min(change, changes[j][i])
  if i < W-1:
    que.append((i+1, j, current, change))
  if j < H-1:
    que.append((i, j+1, current, change))
print(changes[-1][-1])
    


### FIXED CODE ###

from collections import deque
H, W = [int(i) for i in input().split()]
Shw = [[0 if i =="." else 1 for i in input()] for _ in range(H)]

changes = [[-1 for _ in range(W)] for _ in range(H)]

for j in range(H):
  for i in range(W):
    current = Shw[j][i]
    if i == 0 and j ==0:
      changes[j][i] = current
    elif i == 0:
      changes[j][i] = changes[j-1][i] + (1 if current == 1 and Shw[j-1][i] == 0 else 0)
    elif j == 0:
      changes[j][i] = changes[j][i-1] + (1 if current == 1 and Shw[j][i-1] == 0 else 0)
    else:
      upper = changes[j-1][i] + (1 if current == 1 and Shw[j-1][i] == 0 else 0)
      lower = changes[j][i-1] + (1 if current == 1 and Shw[j][i-1] == 0 else 0)
      changes[j][i] = min(upper, lower)
      
print(changes[-1][-1])
### BUGGY CODE ###

def binary_search(key, l):
    ix = len(l) // 2

    while len(l) > 0:
        if key == l[ix]:
            return True

        if key < l[ix]:
            l = l[:ix]
        else:
            l = l[ix + 1:]
        ix = len(l) // 2

    return False

if __name__ == '__main__':
    n = input()
    s = [int(x) for x in input().split()]
    q = input()
    t = [int(x) for x in input().split()]

    ans = 0
    for e in t:
        ans += 1 if binary_search(e, s) else 0

    print(ans)

### FIXED CODE ###

def binary_search(key, a):
    l = 0
    r = len(a)
    ix = (r - l) // 2

    while r - l > 0:
        if key == a[ix]:
            return True

        if key < a[ix]:
            r = ix
        else:
            l = ix + 1
        ix = l + (r - l) // 2

    return False

if __name__ == '__main__':
    n = input()
    s = [int(x) for x in input().split()]
    q = input()
    t = [int(x) for x in input().split()]

    ans = 0
    for e in t:
        ans += 1 if binary_search(e, s) else 0

    print(ans)