Harryxun/stf_run huggingface dataset:
from itertools import accumulate
MOD = 10 ** 9 + 7
S = int(input())

# dp[a][b]: 長さaで、合計bである数列の数

dp = [0] * (S+4)
dp[0] = 1
acc = list(accumulate(dp))
acc[-1] = acc[-2] = acc[-3] = 0

ans = 0
for a in range(700):
    ndp = []
    for b in range(S+1):
        ndp.append(acc[b-3] % MOD)
    ans += ndp[S]
    ans %= MOD
    dp = ndp
    acc = list(accumulate(dp))
    acc[-1] = acc[-2] = acc[-3] = 0

print(ans)


### FIXED CODE ###

from itertools import accumulate
MOD = 10 ** 9 + 7
S = int(input())

# dp[a][b]: 長さaで、合計bである数列の数

dp = [0] * (S+4)
dp[0] = 1
acc = list(accumulate(dp))
acc[-1] = acc[-2] = acc[-3] = 0

ans = 0
for a in range(700):
    ndp = []
    for b in range(S+1):
        ndp.append(acc[b-3] % MOD)
    ndp += [0, 0, 0]
    ans += ndp[S]
    ans %= MOD
    dp = ndp
    acc = list(accumulate(dp))
    acc[-1] = acc[-2] = acc[-3] = 0

print(ans)
n, m = map(int, input().split())
mod = 10 ** 9 + 7
a = []
for i in range(m):
    a.append(int(input()))

dp = [-1] * (n + 1)


def dfs(step):
    if dp[step] >= 0:
        return dp[step]
    if step - 1 in a and step - 2 in a:
        return 0
    cnt = 0
    if step <= 0:
        return 1
    if step - 1 >= 0 and step - 1 not in a:
        cnt = (cnt + dfs(step - 1)) % mod
    if step - 2 >= 0 and step - 2 not in a:
        cnt = (cnt + dfs(step - 2)) % mod
    dp[step] = cnt
    return dp[step]


print(dfs(n))


### FIXED CODE ###

n, m = map(int, input().split())
mod = 10 ** 9 + 7
pat = [0] * (n + 2)
pat[0] = 1
pat[1] = 1

for i in range(m):
    a = int(input())
    pat[a] = -1

for i in range(2, n + 1):
    if pat[i] < 0:
        continue
    if pat[i - 1] > 0:
        pat[i] = (pat[i] + pat[i - 1]) % mod
    if pat[i - 2] > 0:
        pat[i] = (pat[i] + pat[i - 2]) % mod

print(pat[n])
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

# print(N, A_LIST, B_LIST, C_LIST)

result = 0

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

print(result)
# 5+8+9+24+15+13


### FIXED CODE ###

N = int(input())
print(N ** 3)
n=int(input())
c=""
while n:
    n-=1
    c=chr(n%26+97)+c
    n//=26
print(c)

### FIXED CODE ###

s=input()
print("A"if s==s.upper()else"a")
pqr = list(map(int, input.split()))
print(sum(pqr) - max(pqr))

### FIXED CODE ###

pqr = list(map(int, input().split()))
print(sum(pqr) - max(pqr))
N = int(input())
ab = [list(map(int, input().split())) for _ in range(max(1, N-1))]
c = list(map(int, input().split()))

if N == 1:
    print(0)
    print(c[0])
    exit()

c.sort(reverse = True)
edge = [[] for _ in range(N)]
for a, b in ab:
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1]
search = [0]
res = 0
rese = [None for _ in range(N)]
for i in search:
    count = 0
    ans = 0
    anse = [None for _ in range(N)]
    anse[i] = c[0]
    dfs(i, -1)
    if res < ans:
        res = ans
        rese = anse
print(res)
print(" ".join(map(str, rese)))


### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N-1)]
c = list(map(int, input().split()))

if N == 1:
    print(0)
    print(c[0])
    exit()

c.sort(reverse = True)
edge = [[] for _ in range(N)]
for a, b in ab:
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1]
search = [0]
res = 0
rese = [None for _ in range(N)]
for i in search:
    count = 0
    ans = 0
    anse = [None for _ in range(N)]
    anse[i] = c[0]
    dfs(i, -1)
    if res < ans:
        res = ans
        rese = anse
print(res)
print(" ".join(map(str, rese)))
n, k = map(int, input().split())

cnt = [0] * (n)

for i in range(1, n + 1):
    cnt[i % k] += 1

if k % 2 == 0:
    ans = 0
    ans += cnt[0] * cnt[0] * cnt[0]
    ans += cnt[k // 2] * cnt[k // 2] * cnt[k // 2]
    print(ans)
else:
    print(cnt[0] * cnt[0] * cnt[0])



### FIXED CODE ###

n, k = map(int, input().split())

cnt = [0] * (n + 1)

for i in range(1, n + 1):
    cnt[i % k] += 1

if k % 2 == 0:
    ans = 0
    ans += cnt[0] * cnt[0] * cnt[0]
    if k // 2 <= n:
        ans += cnt[k // 2] * cnt[k // 2] * cnt[k // 2]
    print(ans)
else:
    print(cnt[0] * cnt[0] * cnt[0])
A, B = map(int, input().split())
lst = [['#' for j in range(100)] for i in range(50)] + [['.' for j in range(100)] for i in range(50)]
for i in range(A-1):
	a, b = divmod(i, 50)
    lst[a*2][b*2] = '.'
for i in range(B-1):
    a, b = divmod(i, 50)
    lst[-1-a*2][-1-b*2] = '#'
print(100, 100)
for j in lst:
    print(''.join(j))

### FIXED CODE ###

A, B = map(int, input().split())
lst = [['#' for j in range(100)] for i in range(50)] + [['.' for j in range(100)] for i in range(50)]
for i in range(A-1):
    a, b = divmod(i, 50)
    lst[a*2][b*2] = '.'
for i in range(B-1):
    a, b = divmod(i, 50)
    lst[-1-a*2][-1-b*2] = '#'
print(100, 100)
for j in lst:
    print(''.join(j))
n, m, q = map(int, input().split())
abcd = []
for i in range(q):
    abcd.append(list(map(int, input().split())))

A = []


def dfs(num):
    if len(str(num)) == n:
        A.append(num)
    else:
        now = int(str(num)[-1])
        for i in range(now, m + 1):
            dfs(int(str(num) + str(i)))


for i in range(1, 10):
    dfs(i)

ans = 0
for i in range(len(A)):
    now = str(A[i])
    point = 0
    for j in range(q):
        a, b, c, d = abcd[j]
        a -= 1
        b -= 1
        if int(now[b]) - int(now[a]) == c:
            point += d

    ans = max(ans, point)

print(ans)


### FIXED CODE ###

n, m, q = map(int, input().split())
abcd = [list(map(int, input().split())) for i in range(q)]

A = []


def dfs(num):
    if len(num) == n:
        A.append(num)
    else:
        now = num[-1]
        for i in range(now, m + 1):
            dfs(num + [i])


for i in range(1, 10):
    dfs([i])

ans = 0
for now in A:
    point = 0
    for a, b, c, d in abcd:
        if now[b-1] - now[a-1] == c:
            point += d

    ans = max(ans, point)

print(ans)
n,a,b = map(int, input())
out = 0

for i in range(0,n+1):
  sum = sum([int(j) for j in str(j)])
  if a <= sum and sum <= b:
    out += i

print(i)

### FIXED CODE ###

n,a,b = map(int, input().split())
out = 0
 
for i in range(0,n+1):
  s = sum([int(j) for j in str(i)])
  if a <= s and s <= b:
    out += i
 
print(out)
W = input()

if W[0] == "S":
  print("Cloudy")
else if W[0] == "C":
  print("Rainy")
else:
  print("Sunny")
  

### FIXED CODE ###

W = input()
 
if W[0] == "S":
  print("Cloudy")
elif W[0] == "C":
  print("Rainy")
else:
  print("Sunny")
A=int(input())
B=int(input())
if A>1 and B>1:
  C=(A-1)*(B-1)
else:
  break
print(C)

### FIXED CODE ###

A,B=list(map(int,input().split()))
C=(A-1)*(B-1)
print(C)
n,m = map(int, input().split())
a = n**m%1,000000007
print(a)

### FIXED CODE ###

def power(n,m):
    if m == 0:
        return 1
    if m % 2 == 0:
        return power(n*n%1000000007,m//2)
    else:
        return n * power(n,m-1) % 1000000007
        
n,m = map(int, input().split())
print(power(n,m))
while True:
    n, m = map(int, input().split())
    ls = []
    if n == 0: break
    for i in range(n):
        d, p = map(int, input().split())
        ls.append([d,p])
    ls.sort(key=lambda x:x[1], reverse=True)
    j = 0
    for i in range(m):
        ls[j][0] -= 1
        if ls[j][0] == 0:
            j += 1
            if j == (len(ls)):
                break
    print(sum(x[0] * x[1] for x in ls))

### FIXED CODE ###

while True:
    n, m = map(int, input().split())
    ex = [0] * 11
    if n == 0: break
    for i in range(n):
        d, p = map(int, input().split())
        ex[p] += d
    ex.reverse()
    for i in range(len(ex)):
        m -= ex[i]
        if m <= 0:
            ex[i] = abs(m)
            break
        else:
            ex[i] = 0
    ex.reverse()
    ans = 0
    for i in range(len(ex)):
        ans += ex[i] * i
    print(ans)
from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)
def comb(n,r):
    return factorial(n)//(factorial(n-r)*factorial(r))
n,a,b = map(int,input().split())
v = list(map(int,input().split()))
v.sort(reverse=True)
max_mean = sum(v[0:a])/a
print(max_mean)
#選び方の総数を求める
c = Counter(v)
i = 0
num = 0
ans = 0
while num<=a:
    if num+c[v[i]]>=a:
        num += c[v[i]]
    else:
        i +=1
        num += c[v[i]]
if i > 0:
    ans += comb(c[v[i]],a)
    print(ans)
else:
    for i in range(a,b+1):
        if c[v[0]] >=i:
            ans += comb(c[v[0]],i)
    print(ans)

### FIXED CODE ###

from itertools import*
from math import*
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = 10**18
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)
def comb(n,r):
    if r > n:
        return 0
    else:
        return factorial(n)//(factorial(n-r)*factorial(r))
n,a,b = map(int,input().split())
v = list(map(int,input().split()))
v.sort(reverse=True)
max_mean = sum(v[0:a])/a
print(max_mean)
#選び方の総数を求める
c = Counter(v)
i = 0
num = 0
ans = 0
while num<a:
    if num+c[v[i]]>=a:
        num += c[v[i]]
    else:
        num += c[v[i]]
        i += 1
num -= c[v[i]]

if i > 0:
        ans += comb(c[v[i]],a-num)
        print(ans)
else:
    for j in range(a,b+1):
        if c[v[0]] >=j:
            ans += comb(c[v[0]],j)
    print(ans)
a = int(input())
b = int(input())

if a < b:
  print("a < b")
elif a == b:
  print("a == b")
else:
  print("a > b")

### FIXED CODE ###

x = input().split()
i = list(map(int,x))
a = i[0]
b = i[1]

if a > b:
  print('a > b')
elif a == b:
  print('a == b')
else:
  print('a < b')
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
ab=[list(map(int,input().split())) for i in range(n-1)]
ki=[[] for i in range(n+1)]
for i in range(n-1):
    ki[ab[i][0]].append(ab[i][1])
    ki[ab[i][1]].append(ab[i][0])
# print(ki)
kyori=[[0,0] for i in range(n+1)]
def tansaku(m, dis, hito):
    if not ki[m]:
        kyori[m][hito]=dis
        return
    elif kyori[m][hito]!=0:
        return
    else:
        kyori[m][hito]=dis
        for i in range(len(ki[m])):
            tansaku(ki[m][-1-i], dis+1, hito)
tansaku(1,1,0)
tansaku(n,1,1)
fe=0
su=0
for i in range(n+1):
    if kyori[i][0]<kyori[i][1]:
        fe+=1
    elif kyori[i][0]>kyori[i][1]:
        su+=1
    else:
        pass
if fe>su:
    print("Fennec")
else:
    print("Snuke")

### FIXED CODE ###

import sys
sys.setrecursionlimit(2000000)
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
ab=[list(map(int,input().split())) for i in range(n-1)]
ki=[[] for i in range(n+1)]
for i in range(n-1):
    ki[ab[i][0]].append(ab[i][1])
    ki[ab[i][1]].append(ab[i][0])
# print(ki)
kyori=[[float("INF"), float("INF")] for i in range(n+1)]
def tansaku(m, dis, hito):
    q=[[m,dis]]
    while q:
        po=q.pop()
        for i in ki[po[0]]:
            if kyori[i][hito]>po[1]:
                kyori[i][hito]=po[1]
                q.append([i,po[1]+1])

tansaku(1,1,0)
tansaku(n,1,1)
fe,su=0,0

for i in range(1,n+1):
    if kyori[i][0]<=kyori[i][1]:
        fe+=1
    else:
        su+=1

if fe>su:
    print("Fennec")
else:
    print("Snuke")
X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort()

p = p[:X]
q = q[:Y]
z = []

while len(r) != 0 and r[-1] > min(p[-1], q[-1]):
    k = r.pop()
    if p[-1] < q[-1]:
        del p[-1]
    else:
        del q[-1]
    z.append(k)   

print(sum(p) + sum(q) + sum(z))

### FIXED CODE ###

X, Y, A, B, C = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))

p.sort(reverse = True)
q.sort(reverse = True)
r.sort()

p = p[:X]
a = q[:Y] + p
z = []
a.sort(reverse = True)

while len(a) != 0 and len(r) != 0 and r[-1] > a[-1]:
    k = r.pop()
    del a[-1]
    z.append(k)   

print(sum(a) + sum(z))
n = int(input())
l = list(map(int,input().split()))
l1 = [0,0]
from collections import Counter
c = Counter(l)
for i in c.items():
  if i[1]>=2 :
    l1.append(i[0])
sort.(l1)
print(l1[-1]*l1[-2])

### FIXED CODE ###

n = int(input())
l = list(map(int,input().split()))
l1 = [0,0]
from collections import Counter
c = Counter(l)
for i in c.items():
  if i[1]>=2 :
    l1.append(i[0])
  if i[1]>=4 :
    l1.append(i[0])
l1.sort()
print(l1[-1]*l1[-2])
i = 0
x = int(input())
while x != 0:
	i += 1
	print("Case", i, ":", x, sep="")
	x = int(input())
	

### FIXED CODE ###

i = 0
x = int(input())
while x != 0:
	i += 1
	print("Case ", i, ": ", x, sep="")
	x = int(input())
#! -*- coding:utf-8 -*-

def search(values,hp,vp,item):
	if not (0<=hp<len(values)): return
	if not (0<=vp<len(values[hp])): return
	if item!=values[hp][vp]: return
	values[hp][vp]=True
	search(values,hp-1,vp,item)
	search(values,hp+1,vp,item)
	search(values,hp,vp-1,item)
	search(values,hp,vp+1,item)

def solve(values):
	count=0
	for i in range(len(values)):
		for j in range(len(values[i])):
			if values[i][j] in ['@','#','*']:
				search(values,i,j,values[i][j])
				count+=1
	return count

def main():
	line,values=input(),list()
	while line!='0 0':
		H,W = list(map(int,line.strip().split(' ')))
		value = list()
		for _ in range(H):
			value.append([x for x in input().strip()])
		values.append(value)
		line = input()
	for value in values:
		print solve(value)

if __name__=='__main__':
	main()

### FIXED CODE ###

# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**6)

def search(field,hp,vp,item):
	if not (0<=hp<len(field)): return
	if not (0<=vp<len(field[hp])): return
	if item!=field[hp][vp]: return
	field[hp][vp]=True
	for dh,dv in [[0,1],[0,-1],[1,0],[-1,0]]:
		search(field,hp+dh,vp+dv,item)
#	search(field,hp-1,vp,item)
#	search(field,hp+1,vp,item)
#	search(field,hp,vp-1,item)
#	search(field,hp,vp+1,item)

def solve(field):
	count,valid_items=0,set(['@','#','*'])
	for i in range(len(field)):
		for j in range(len(field[i])):
			if field[i][j] in valid_items:
				search(field,i,j,field[i][j])
				count+=1
	return count

def main():
	line = input().strip()
	while line!='0 0':
		H,W = list(map(int,line.split(' ')))
		field = list()
		for _ in range(H):
			field.append(list(input().strip()))
		print(solve(field))
		line = input().strip()

if __name__=='__main__':
	main()
X = int(input())
if X = 3:
  print("YES")
elif X = 5:
  print("YES")
elif X = 7:
  print("YES")
else:
  print("NO")

### FIXED CODE ###

X = int(input())
if X == 3:
  print("YES")
elif X == 5:
  print("YES")
elif X == 7:
  print("YES")
else:
  print("NO")
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
lrc=[list(map(int,input().split())) for _ in range(m)]
lrc.sort(key=lambda x:x[0])
rr=1
ll=10**5+1
for l,r,c in lrc:
    if rr<l:
        print(-1)
        exit()
    ll=min(l,ll)
    rr=max(rr,r)

if not (ll==1 and rr==n):
    print(-1)
    exit()

lrc.sort(key=lambda x:x[1])
l=[1]
r=[1]
c=[0]
for i in range(m):
    ll,rr,cc=lrc[i]
    l.append(ll)
    r.append(rr)
    c.append(cc)

import bisect

for i in range(1,m+1):
    x=bisect.bisect_left(r,l[i])
    if x>=i:
        continue
    c[i]+=c[x]

x=bisect.bisect_left(r,n)
if x==m:
    print(-1)
    exit()
ans1=min(c[x:])

lrc.sort(key=lambda x:x[0])
l=[1]
r=[1]
c=[0]
for i in range(m):
    ll,rr,cc=lrc[i]
    l.append(ll)
    r.append(rr)
    c.append(cc)

import bisect

for i in range(1,m+1):
    x=bisect.bisect_left(r,l[i])
    if x>=i:
        continue
    c[i]+=c[x]

x=bisect.bisect_left(r,n)
if x==m:
    print(-1)
    exit()
ans2=min(c[x:])

ans=min(ans1,ans2)
print(ans)


### FIXED CODE ###


from collections import defaultdict
from heapq import heappop, heappush


class Graph():

    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()


class Dijkstra():

    def __init__(self, graph, start):
        self.g = graph.graph

        self.dist = defaultdict(lambda: float("inf"))
        self.dist[start] = 0

        self.prev = defaultdict(lambda: None)

        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            dist_u, u = heappop(self.Q)

            if self.dist[u] < dist_u:
                continue

            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self, goal):

        return self.dist[goal]

    def shortest_path(self, goal):
        path = []
        node = goal

        while node is not None:
            path.append(node)
            node = self.prev[node]

        return path[::-1]


import sys
input=sys.stdin.readline
n,m=map(int,input().split())
g=Graph()

for _ in range(m):
    l,r,c=map(int,input().split())
    g.add_edge(l,r,c)
for i in range(2,n+1):
    g.add_edge(i,i-1,0)


d = Dijkstra(g,1)
ans=d.shortest_distance(n)
print(ans if ans!=float("inf") else -1)
A, B, C = map(int, input().split())
K= int(input()) 
 
 
for i in range(1,K+1):
  B=B*2
  if (B>A)&(C>B):
    print("Yes")
  else :
    print("No")

### FIXED CODE ###

s = int(input()) 
 
if 400 <= s <= 599:
  print("8")
elif  600 <= s <=799:
        print("7")
elif  800 <= s <=999:
        print("6")
elif  1000 <= s <=1199:
        print("5")
elif  1200 <= s <=1399:
        print("4")
elif  1400 <= s <=1599:
        print("3")
elif  1600 <= s <=1799:
        print("2")
elif  1800 <= s <=1999:
        print("1")
a, b = map(int, input().split())
if a%2 == 1 and b% == 1:
  print('Odd')
else:
  print('Even')

### FIXED CODE ###

a, b = map(int, input().split())
if a%2 == 1 and b%2 == 1:
  print('Odd')
else:
  print('Even')
n = int(input())
a_array = list(map(int(), input().split()))
b_array = list(map(int(), input().split()))
c_array = list(map(int(), input().split()))

c_array.append(0)

sum = 0

for m in a_array:
    sum += b_array[m-1]

for i in range(n-1):
    if (a_array[i] + 1 == a_array[i+1]):
        sum += c_array[a_array[i]-1]

print(sum)


### FIXED CODE ###

n = int(input())
a_array, b_array, c_array = [list(map(int, input().split())) for _ in range(3)]
c_array.append(0)

sum = 0

for m in a_array:
    sum += b_array[m-1]

for i in range(n-1):
    if (a_array[i] + 1 == a_array[i+1]):
        sum += c_array[a_array[i]-1]

print(sum)
def partial_add(p, x, tree, value):
    value[p] += x
    if p in tree.keys():
        for root in tree[p]:
            partial_add(root, x, tree, value)
    return


N, Q = map(int, input().split())
tree = {}
value = [0] * (N+1)

for _ in range(N-1):
    a, b = map(int, input().split())
    try:
        tree[a].append(b)
    except KeyError:
        tree[a] = [b]

for i in range(Q):
    p, x = map(int, input().split())
    partial_add(p, x, tree, value)

print(*value[1:])


### FIXED CODE ###

import sys


sys.setrecursionlimit(300000)

to = [[] for _ in range(200005)]
ans: list


def dfs(v, p=-1):
    global ans
    for u in to[v]:
        if u == p: continue
        ans[u] += ans[v]
        dfs(u, v)
    return


def main():
    N, Q = map(int, input().split())
    for _ in range(N-1):
        a, b = map(int, input().split())
        to[a-1].append(b-1)
        to[b-1].append(a-1)
    global ans
    ans = [0] * N
    for _ in range(Q):
        p, x = map(int, input().split())
        ans[p-1] += x
    dfs(0)
    print(*ans)


if __name__ == "__main__":
    main()
N=int(input())
a=list(map(int,input().split()))
count=0
for i in a:
    while b%2==0:
        b/=2
        count+=1
print(count)

### FIXED CODE ###

A,B=map(int,input().split())
if A>8 or B>8:
    print(':(')
else:
    print('Yay!')
from itertools import chain

N, K = map(int, input().split())
P_L = [int(x)-1 for x in input().split()]
C_L = [int(x) for x in input().split()]

s_l = [[0] * N for _ in range(N+1)]

for i in range(N):
    _p = i
    for j in range(1, N+1):
        _p = P_L[_p]
        s_l[j][i] = s_l[j-1][i] + C_L[_p]

if K <= N:
    _l = list(chain(*(s_l[:K+1])))
    print(max(_l[N:]))
else:
    _tmp = s_l[N][0]
    _l = list(chain(*s_l))
    if _tmp <= 0:
        print(max(_l[N:]))
    else:
        _c = K // N
        _n = K % N
        _ans_a = _tmp * _c + max(_l[N:_n*N])
        _ans_b = _tmp * (_c - 1) + max(_l[N:])
        print(max(_ans_a, _ans_b))


### FIXED CODE ###

N, K = map(int, input().split())
P_L = [int(x)-1 for x in input().split()]
C_L = [int(x) for x in input().split()]

ans = float("-Inf")
for s in range(N):
    _s_l = []
    _v = s
    _cumsum = 0
    _ans = float("-Inf")
    while True:
        _nextv = P_L[_v]
        _cumsum += C_L[_nextv]
        _s_l.append(_cumsum)
        _v = _nextv
        if _nextv == s:
            break
    if K <= len(_s_l):
        _ans = max(_s_l[:K])
    else:
        if _s_l[-1] < 0:
            _ans = max(_s_l)
        else:
            _l = len(_s_l)
            if K % _l == 0:
                _ans = max(_s_l[-1] * (K // _l),
                           _s_l[-1] * (K // _l - 1) + max(_s_l))
            else:
                _ans = max(_s_l[-1] * (K // _l) + max(_s_l[:K % _l]),
                           _s_l[-1] * (K // _l - 1) + max(_s_l))
    ans = max(ans, _ans)

print(ans)
while True:
        a,op,b=map(input().split())
        a=int(a)
        b=int(b)
                
        if op==("+"):
                print(a+b)
        elif op==("-"):
                print(a-b)
        elif op==("*"):
                print(a*b)
        elif op==("/"):
                print(a/b)
        elif op==("?"):
                break

~                                         


### FIXED CODE ###



while 1:
     s=input()
     if '?' in s: break
     print(int(eval(s)))
from collections import deque
S=deque(input())
q=int(input())
for i in range(q):
  c=list(input().split())
  if c[0]=='1':
    S.reverse()
  elif c[1]=='1':
    S.appendleft(c[2])
  elif c[1]=='2':
    S.append(c[2])
ans=''.join(S)
print(ans)


### FIXED CODE ###

a=input()
if len(set(a))==1:
  print("No")
else:
  print("Yes")
print( 'Yes' if len(collections.Counter(map(int,input().split())).items()) == 1 else 'No' )

### FIXED CODE ###

import collections
print( 'Yes' if len(collections.Counter(map(int,input().split())).items()) == 1 else 'No' )
N = int(input())
d = list(map(int,input().split()))
sum = 0
for i in range(N-1):
    for j in range(i+1,N):
        sum += d[i] + d[j]
print(sum)


### FIXED CODE ###

A,B = map(int,input().split())
ans = A-B*2
if ans < 0:
    ans = 0
print(ans)
a=int(input())
b=int(input())
area=a*b
shu=2(a+b)
print(area, shu)

### FIXED CODE ###

a,b=map(int, input().split())
print(a*b, 2*(a+b))
a,b,c,d=map(int,input().split())
import math
nc=math.floor(b/c)-math.ceil(a/c)+1
nd=math.floor(b/d)-math.ceil(a/d)+1
min=c*d//math.gcd(c,d)
ncd=math.floor(b/min)-math.ceil(a/min)+1
print(b-a+1-(nc+nd-ncd))

### FIXED CODE ###

a,b,c,d=map(int,input().split())
import fractions
import math
nc=b//c-(a-1)//c
nd=b//d-(a-1)//d
min=c*d//fractions.gcd(c,d)
ncd=b//min-(a-1)//min
print(b-a+1-(nc+nd-ncd))
n=list(map(int,input().split()))
print(n.index(0)+1)

### FIXED CODE ###

n=list(map(int,input().split()))
print(n.index(0)+1)