Harryxun/stf_run huggingface dataset:
import collections
I = [int(_) for _ in open(0).read().split()]
N = I[0]
AB = I[1:]
G = collections.defaultdict(set)
for a, b in zip(AB[::2], AB[1::2]):
    G[a].add(b)
    G[b].add(a)
color = [None] * (N + 1)


def dfs(G, v, cur):
    color[v] = cur
    for w in G[v]:
        if color[w] is None:
            dfs(G, w, 1 - cur)


dfs(G, 1, 0)
c=collections.Counter(color[1:])
cand1 = set(range(1, N + 1, 3))
cand2 = set(range(2, N + 1, 3))
cand3 = set(range(3, N + 1, 3))

i0, i1 = 0, 1
if c[0] > c[1]:
    i0, i1 = 1, 0

ans = [None] * (N + 1)
if c[i0] <= len(cand3):
    for i in range(1, N + 1):
        if color[i]==i0:
            ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand2:
                ans[i] = cand2.pop()
            else:
                ans[i] = cand3.pop()
else:
    for i in range(1, N + 1):
        if color[i] == i0:
            if cand2:
                ans[i] = cand2.pop()
            else:
                ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            else:
                ans[i] = cand3.pop()
print(*ans[1:])


### FIXED CODE ###

import collections
import sys
sys.setrecursionlimit(2*10**5)
I = [int(_) for _ in open(0).read().split()]
N = I[0]
AB = I[1:]
G = collections.defaultdict(set)
for a, b in zip(AB[::2], AB[1::2]):
    G[a].add(b)
    G[b].add(a)
color = [None] * (N + 1)


def dfs(G, v, cur):
    color[v] = cur
    for w in G[v]:
        if color[w] is None:
            dfs(G, w, 1 - cur)


dfs(G, 1, 0)
c=collections.Counter(color[1:])
cand1 = set(range(1, N + 1, 3))
cand2 = set(range(2, N + 1, 3))
cand3 = set(range(3, N + 1, 3))

i0, i1 = 0, 1
if c[0] > c[1]:
    i0, i1 = 1, 0

ans = [None] * (N + 1)
if c[i0] <= len(cand3):
    for i in range(1, N + 1):
        if color[i]==i0:
            ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
else:
    for i in range(1, N + 1):
        if color[i] == i0:
            if cand2:
                ans[i] = cand2.pop()
            elif cand3:
                ans[i] = cand3.pop()
        else:
            if cand1:
                ans[i] = cand1.pop()
            elif cand3:
                ans[i] = cand3.pop()
print(*ans[1:])
N= map(int, input().split())
A = list(map(int, input().split()))

# if N >= sum(A):
#     print(N-sum(A))
# else:
#     print(-1)
for x in A:
    count[i] +=1
for i in range(1,N+1):
    print(count[i])

### FIXED CODE ###

# R = int(input()) 
N= int(input())
A = list(map(int, input().split()))



# if N >= sum(A):
#     print(N-sum(A))
# else:
#     print(-1)
count= [0] * N

for x in A:
    count[x-1] +=1
for i in range(N):
    print(count[i])

# def count_defaultdict(it):
#     counter = collections.defaultdict(int)
#     for x in it:
#         counter[x] += 1
#     return dict(counter)
from collections import deque

def a():
    H,W = map(int,input().split())
    S = ["" for _ in range(H)]
    for _ in range(H):
        S[_] = input()
    b = lambda n:S[n//W][n%W]
    u = (lambda n:(n%W,n//W))(min(range(H*W),key=b))

    def bfs(root):
        depth = [[-1]*W for _ in range(H)]
        q = deque()
        q.append((root,0))
        while q:
            r,d = q.popleft()
            x,y = r
            depth[y][x] = d
            for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)):
                dx += x
                dy += y
                if 0 <= dx < W and 0 <= dy < H and depth[dy][dx] == -1 and S[dy][dx] == ".":
                    q.append(((dx,dy),d+1))
        return depth

    c = bfs(u)
    d = lambda n:c[n//W][n%W]
    v = (lambda n:(n%W,n//W))(max(range(H*W),key=d))

    e = bfs(v)
    f = lambda n:e[n//W][n%W]
    print(f(max(range(H,W),key=f)))



a()





### FIXED CODE ###

from collections import deque

def a():
    H,W = map(int,input().split())
    S = ["" for _ in range(H)]
    for _ in range(H):
        S[_] = input()
    b = lambda n:S[n//W][n%W] == '.'
    v = list(filter(b,range(H*W)))
    u = list(map(lambda n:(n%W,n//W),v))

    le = len(v)

    def bfs(root):
        i = 0
        depth = [[-1]*W for _ in range(H)]
        in_que = [[0]*W for _ in range(H)]
        q = deque()
        q.append((root,0))
        xx,yy = root
        in_que[yy][xx] = 1
        while i < le:
            r,d = q.popleft()
            x,y = r
            depth[y][x] = d
            i += 1
            for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)):
                dx += x
                dy += y
                if 0 <= dx < W and 0 <= dy < H and in_que[dy][dx] == 0 and S[dy][dx] == ".":
                    q.append(((dx,dy),d+1))
                    in_que[dy][dx] = 1
        return depth
    
    print(max([max([max(y) for y in x]) for x in map(bfs,u)]))

a()
(N,K)=map(int,input().split())
v=list(map(int, input().split()))

z=0
for i in range(v):
    if i>=K:
         z=z+1

print(z)

### FIXED CODE ###

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

z=0
for i in v:
    if i>=K:
         z=z+1

print(z)
import collections

n = int(input())
a = list(map(int,input().split()))
c = dict(collections.Counter(a))

d1 = []
d2 = []
for num,v in c.items():
  if v >= 2:
    d1.append(num)
  if v >= 4:
    d2.append(num)
    
d1.sort()
d2.sort()

if d2:
  ans = max(d2[-1]**2, d1[-1]*d1[-2])
  print(ans)
else:
  print(d1[-1]*d1[-2])
  



### FIXED CODE ###

import collections

n = int(input())
a = list(map(int,input().split()))
c = dict(collections.Counter(a))

d1 = []
d2 = []
for num,v in c.items():
  if v >= 2:
    d1.append(num)
  if v >= 4:
    d2.append(num)
    
d1.sort()
d2.sort()

if d2:
  ans = max(d2[-1]**2, d1[-1]*d1[-2])
  print(ans)
else:
  if len(d1)>=2:
    print(d1[-1]*d1[-2])
  else:
    print(0)
N = int(input())
L = [0] * (100000+1)
for i in range(N):
    a = int(input())
    L[a] = L[a]+1
cnt = 0
for x in L:
    if x %2 == 1:
        cnt += 1
print(cnt)

### FIXED CODE ###

import collections

N = int(input())
L = [int(input()) for i in range(N)]

M = collections.Counter(L)
C = M.most_common()
cnt = 0

for x in C:
    if x[1] % 2 == 1:
        cnt += 1
        
print(cnt)
l = 'abcdefghijklmnopqrstuvwxyz'
mp = [0 for _ in list(l)]

while True:
    in_data = list(filter(lambda x: 97 <= ord(x) <= 122 , raw_input().lower()))

    for s in in_data:
        mp[ord(s) - 97] += 1

    for i,j in zip(l, mp):
        print "%s : %s" % (i, j)

### FIXED CODE ###

l = 'abcdefghijklmnopqrstuvwxyz'
mp = [0 for _ in list(l)]

while True:
    try:
        in_data = list(filter(lambda x: 97 <= ord(x) <= 122 , raw_input().lower()))
    except:
        break

    for s in in_data:
        mp[ord(s) - 97] += 1

for i,j in zip(l, mp):
    print "%s : %s" % (i, j)
ans = a-b-c
if ans < 0:
    print(ans*-1)
else:
    print(0)

### FIXED CODE ###

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

ans = a-b-c
if ans < 0:
    print(ans*-1)
else:
    print(0)
#166d
x = int(input())
for i in range(-73,73):
    for j in range(-73,73):
        if i**5-j**5==x:
            a = i
            b = j
            break
        
print(a,b)

### FIXED CODE ###

#166d
x = int(input())
for i in range(-120,120):
    for j in range(-120,120):
        if i**5-j**5==x:
            a = i
            b = j
            break
        
print(a,b)
import math
def solve(input_text):
    for line in input_text:
        op=line[1]
        a=int(line[0])
        b=int(line[2])
        ans=0
        
        if op=='+':
            ans=a+b
        if op=='-':
            ans=a-b
        if op=='*':
            ans=a*b
        if op == '/':
            ans =math.float(a/b)
        print(ans)
    
def answer():
    input_text=[]
    line=[]
    
    while True:
        
        line = input()
        line= line.split(' ')
        if line[1]=='?':
            break
        input_text.append(line)
    
    solve(input_text)

if __name__ =='__main__':
    answer()


### FIXED CODE ###

import math
def solve(input_text):
    for line in input_text:
        op=line[1]
        a=int(line[0])
        b=int(line[2])
        ans=0
        
        if op=='+':
            ans=a+b
        if op=='-':
            ans=a-b
        if op=='*':
            ans=a*b
        if op == '/':
            ans =math.floor(a/b)
        print(ans)
    
def answer():
    input_text=[]
    line=[]
    
    while True:
        
        line = input()
        line= line.split(' ')
        if line[1]=='?':
            break
        input_text.append(line)
    
    solve(input_text)

if __name__ =='__main__':
    answer()
N = input()
x = sum(int(i) for i in N)
print("Yes" if N % x == 0 else "No")


### FIXED CODE ###

N = input()
x = sum(int(i) for i in N)
print("Yes" if int(N) % x == 0 else "No")
A, B = int(map, input().split())
if A <= B:
  if A == 1:
    print('Alice')
  else:
    print('Bob')
else:
  if B == 1:
    print('Bob')
  else:
    print('Alice')
  


### FIXED CODE ###

A, B = map(int, input().split())
if A < B:
  if A == 1:
    print('Alice')
  else:
    print('Bob')

elif A > B:
  if B == 1:
    print('Bob')
  else:
    print('Alice')
else:
  print('Draw')
import sys
input = sys.stdin.readline
#-------------
n,k = map(int,input().split())
a = list(map(int,input().split()))
#-------------
a.sort(reverse = True)
w = 10**9 + 7
ans = 1
if k%2 == 1:#kは奇数
  for i in range(k):
    ans *= a[i]
  print(ans % w)

else:#kは偶数
  if n%2 == 0:#nは偶数
    A = n//2
    a_new = [a[2*i]*a[2*i+1] for i in range(A)]
    a_new.sort(reverse = True)
    for j in range(k//2):
      ans *= a_new[j]
    print(ans % w)
      
  else:#nは奇数
    a_plus =[]
    a_minus = []
    pos = 0
    neg = 0
    for i in range(n):
      if a[i] > 0 :
        a_plus.append(a[i])
        pos += 1
      else :
        a_minus.append(a[i])
        neg += 1
    a_plus.sort(reverse = True)
    a_minus.sort()
    a_plus_new = [a_plus[2*i]*a_plus[2*i+1] for i in range(pos//2)]
    a_minus_new = [a_minus[2*i]*a_minus[2*i+1] for i in range(neg//2)]
    a_new = a_plus_new + a_minus_new
    a_new.sort(reverse = True)
    for j in range(k//2):
      ans *= (a_new[j]%mod)
    print(ans % w)

### FIXED CODE ###

import sys
 
n, k = map(int, input().split())
a = [int(x) for x in input().split()]
mod = pow(10, 9)+7
zero = 0
plus = []
minus = []
for i in range(n):
  if a[i] == 0:
    zero += 1
  elif a[i] > 0:
    plus.append(a[i])
  else:
    minus.append(a[i])
 
P, M = len(plus), len(minus)
plus.sort(reverse=True)
minus.sort()
ans = 1
if n == k:
  for i in range(n):
    ans *= a[i]
    ans %= mod
elif P+M < k:
  ans = 0
elif P == 0:
  if zero and k%2:
    ans = 0
  else:
    if k%2:
      minus.reverse()
    for i in range(k):
      ans *= minus[i]
      ans %= mod
else:
  q = 0
  if k%2:
    ans *= plus[0]
    ans %= mod
    q = 1
  judge = []
  for i in range((len(plus)-q)//2):
    judge.append(plus[2*i+q]*plus[2*i+q+1])
  for i in range(len(minus)//2):
    judge.append(minus[2*i]*minus[2*i+1])
  judge.sort(reverse=True)
  for i in range(k//2):
    ans *= (judge[i]%mod)
    ans %= mod
 
print(ans)
import sys
input = sys.stdin.readline
a,o,b=map(input().split())
a=int(a)
b=int(b)
print(a+b if o=="+" else a-b)


### FIXED CODE ###

import sys
input = sys.stdin.readline
a,o,b=input().split()
a=int(a)
b=int(b)
print(a+b if o=="+" else a-b)
k,n=map(int,input().split())
a=list(map(int,input().split()))
if a[-1]-a[0]<=(k-a[-1])+a[0]:
    print(a[-1]-a[0])
else:
    for i in range(n-1):
        if a[-1]-a[i]>(k-a[-1])+a[i]:
            start=i
        else:
            break
print(((k-a[-1])+a[start])+(a[-1]-a[start+1]))

### FIXED CODE ###

K,N = map(int, input().split())
A = [int(a) for a in input().split()]

ans = A[-1]-A[0]
for i in range(N-1):
    ans = min(ans, K-A[i+1]+A[i])
print(ans)
import itertools
n,*l=map(int,open(0).read().split())
f=lambda g:sorted([[len(list(v)),k]for k,v in itertools.groupby(g)])[-9:]
print(n-max(n//2,max(a+c for c,d in f(l[1::2])for a,b in f(l[::2])if b-d)))

### FIXED CODE ###

from collections import Counter as C
n = int(input())
v = list(map(int,input().split()))
v1,v2 = v[0::2], v[1::2]
a1 = C(v1).most_common()
a2 = C(v2).most_common()

if a1[0][0]!=a2[0][0]:
    print(n-a1[0][1]-a2[0][1])
    exit()

res = n//2
if len(a2)!=1:
    res = a1[0][1] + a2[1][1]
if len(a1)!=1:
    res = max(res, a1[1][1] + a2[0][1])
print(n-res)
N = int(input())
SS = []
for _ in range(N):
    SS.append(sorted(input()))
common = SS[0]
for s in SS:
    sc = s.copy()
    z = []
    for c in common:
        if c in sc:
            sc.remove(c)
            z.append(c)
    common = z
print(''.join(common))


### FIXED CODE ###

N = int(input())
SS = []
for _ in range(N):
    SS.append(sorted(input()))
common = SS[0]
for s in SS:
    sc = s[:]
    z = []
    for c in common:
        if c in sc:
            sc.remove(c)
            z.append(c)
    common = z
print(''.join(common))
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    C = [set() for _ in range(N)]
    E = [[] for _ in range(N)]
    E2 = []
    for _ in range(N-1):
        a, b = map(int, input().split())
        E[a-1].append(b-1)
        E[b-1].append(a-1)
        E2.append([a-1, b-1])
#    print(E)
#    print(E2)
    K = 0
    for e in E:
        K = max(K, len(e))
    print(K)
    s = set()
    for i in range(1, K+1):
        s.add(i)
    for a, b in E2:
        s2 = s.copy()
        s2 -= C[a]
        s2 -= C[b]
        c = s2.pop()
        print(c)
        C[a].add(c)
        C[b].add(c)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
input = sys.stdin.readline


def main():
    N = int(input())
    G = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        G[a-1].append((b-1, i))
        G[b-1].append((a-1, i))

    V = [[-1, -1] for _ in range(N)]  # 頂点の[親, 親への辺の色]
    ans = [-1]*(N-1)
    import collections
    Q = collections.deque()
    Q.append(0)
    while Q:
        i = Q.popleft()
        color = 0
        for c, ei in G[i]:
            if c == V[i][0]:
                continue
            V[c][0] = i
            color += 1
            if color == V[i][1]:
                color += 1
            V[c][1] = ans[ei] = color
            Q.append(c)
    print(max(ans))
    print('\n'.join(map(str, ans)))


if __name__ == '__main__':
    main()
N, M = map(int, input().split())

ds = []
for _ in range(M):
  ds.append(list(map(int, input().split()))[1:])

P = map(int, input().split())

ans = 0

for i in range(2**N):
  switch_states = [(i >> j) & 1 for j in range(N)]
  
  for di, d in enumerate(ds):
    count = 0
    for s in d:
      count += switch_states[s-1]
      if count % 2 == P[di]:
        ans += 1
        
        
print(ans)

### FIXED CODE ###

N, M = map(int, input().split())

ds = []
for _ in range(M):
  ds.append(list(map(int, input().split()))[1:])

P = list(map(int, input().split()))

ans = 0

for i in range(2**N):
  switch_states = [(i >> j) & 1 for j in range(N)]
  
  for di, d in enumerate(ds):
    count = 0
    
    for s in d:
      count += switch_states[s-1]
      
    if count % 2 != P[di]:
      ans -= 1
      break

  ans += 1
        
        
print(ans)
from math import ceil
a = [int(i) for i in input().split()]
if a[1]%2 == 1:
	print ((a[0]//a[1])**3)
else:
	print (math.ceil(a[0]/(a[1]))**3 + ((a[0]//a[1])**3))

### FIXED CODE ###

a = [int(i) for i in input().split()]
 
if a[1]%2 == 1:
	print ((a[0]//a[1])**3)
else:
	if a[0]%a[1] < a[1] // 2:
		print ((a[0]//a[1])**3 + ((a[0]//a[1])**3))
	else:
		print ((1 + a[0]//a[1])**3 + ((a[0]//a[1])**3))
H = int(input())
x = 2
for i in range(41):
  if x <= H and x*2 > H:
    ans = x*2 - 1
    break
  x = x*2
  
if x == 1:
  ans = 1

print(ans)

### FIXED CODE ###

H = int(input())
x = 2
for i in range(41):
  if x <= H and x*2 > H:
    ans = x*2 - 1
    break
  x = x*2
  
if H == 1:
  ans = 1

print(ans)
N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if(1==f(s)) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if(1==f(s)) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive_l(c):
	if c < 0 or N <= c:
		return False

	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0:
				return False
	return True

def isAlive_r(c):
	if c < 0 or N <= c:
		return False

	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if N <= c:
				return False
	return True

S, E = bs_l(isAlive_l, 0, N-1), bs_r(isAlive_r, 0, N-1)
print(E-S+1)


### FIXED CODE ###

N, Q = map(int, input().split())
S = list(input())
TD = [tuple(input().split()) for _ in range(Q)]

def bs_l(f, s, e):
	if s==e:
		return s if f(s) else s+1

	c = (s+e)//2
	if f(c):
		#左
		return bs_l(f, s, c if c == s else c-1)
	else:
		#右
		return bs_l(f, c if c == e else c+1, e)

def bs_r(f, s, e):
	if s==e:
		return s if f(s) else s-1

	c = (s+e)//2
	if not f(c):
		#左
		return bs_r(f, s, c if c == s else c-1)
	else:
		#右
		return bs_r(f, c if c == e else c+1, e)

def isAlive(c):
	for t, d in TD:
		if S[c] == t:
			c += 1 if "R" == d else -1
			if c < 0 or N <= c:
				return False
	return True

S, E = bs_l(isAlive, 0, N-1), bs_r(isAlive, 0, N-1)
print(max(E-S+1, 0))
n = int(input())

for i in range(1,n):
  ans += (n-1)//a
print(ans)

### FIXED CODE ###

n = int(input())
 
ans = 0
for i in range(1,n):
  ans += (n-1)//i
print(ans)
import itertools


def count_ones(n):
  res = []
  length = n.bit_length()
  power = 1
  while len(res) < length:
    d, m = divmod(n, 2**power)
    num_ones = 2**(power-1) * d + max(0, m + 1 - 2**(power-1))
    res.append(num_ones)
    power += 1
  return res

A, B = map(int, input().split())
ones_A = count_ones(A)
ones_B = count_ones(B)

ones = [nb - na for nb, na in itertools.zip_longest(ones_B, ones_A, fillvalue=0)]

change = [x % 2 for x in ones[::-1]]
change = int(''.join(map(str, change)), 2)
print(A ^ change)

### FIXED CODE ###

import itertools


def count_ones(n):
  res = []
  length = n.bit_length()
  power = 1
  while len(res) < length:
    d, m = divmod(n, 2**power)
    num_ones = 2**(power-1) * d + max(0, m + 1 - 2**(power-1))
    res.append(num_ones)
    power += 1
  return res

A, B = map(int, input().split())
if B == 0:
  print(0)
else:
  ones_A = count_ones(A)
  ones_B = count_ones(B)

  ones = [nb - na for nb, na in itertools.zip_longest(ones_B, ones_A, fillvalue=0)]

  change = [x % 2 for x in ones[::-1]]
  change = int(''.join(map(str, change)), 2)
  print(A ^ change)
while 1:
    a, b = map(int, raw_input().split())
    if not a and not b:
        break
    if a < b:
        print "%d %d" %(a, b)
    else:
        print "%d &d" %(b, a)

### FIXED CODE ###

while True:
	a,b = map(int,raw_input().split())
	if a == b == 0:
		break
	if a < b:
		print '%d %d' % (a,b)
	else:
		print '%d %d' % (b,a)
N, M = map(int, input().split)
List = [map(int,input().split()) for i in range(M)]
count = 0

for i in range(N):
  if i+1 >= List[2*i] & i+1 <= List[2*i+1]:
    count = count + 1
print(count)

### FIXED CODE ###

N, M = map(int, input().split())
List_LR = [list(map(int,input().split())) for i in range(M)]
L = [List_LR[i][0] for i in range(len(List_LR))]
R = [List_LR[i][1] for i in range(len(List_LR))]
 
L_max = max(L)
R_min = min(R)

ans = R_min - L_max +1
if ans < 0 : ans = 0
print(ans)
s = input()
count = 0

while S.find("01") != -1 or S.find("10") != -1:
    count += 2 * s.count("01")
    s = s.replace("01", "")
    count += 2 * s.count("10")
    s = s.replace("10", "")
print(count)


### FIXED CODE ###

s = input()
print(2 * min(s.count("0"), s.count("1")))
N,M=map(int,input().split())
high_list=list(map(int,input().split()))
highest=[True]*N
for i in range(M):
    a,b=map(int,input().split()))
    if(high_list[a-1]>=high_list[b-1]):
        highest[b-1]=False
    if(high_list[a-1]<=high_list[b-1]):
        highest[a-1]=False
print(sum(highest))




### FIXED CODE ###

N,M=map(int,input().split())
high_list=list(map(int,input().split()))
highest=[True]*N
for i in range(M):
    a,b=map(int,input().split())
    if(high_list[a-1]>=high_list[b-1]):
        highest[b-1]=False
    if(high_list[a-1]<=high_list[b-1]):
        highest[a-1]=False
print(sum(highest))
N = int(input())
A = list(map(int,input().split()))

ans = 1

if 0 in A:
  ans = 0
else:
  for i in range(N):
    ans *= A[i]
    i += 1
    if ans > 10**18:
      ans = -1
      break

print(ans)


### FIXED CODE ###

a,b = map(int, input().split())
r = a * b
print(r)
n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
now=-100000000000
for i in range(1,n+1):
    retu=[]
    sum1=0
    sum2=0
    sum3=0
    max1=-1000000000000
    max2=-1000000000000
    flag=0
    x=i
    while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
    if n>=k:
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0:
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
    now=max(now,ans)
print(now)

### FIXED CODE ###

n,k=map(int,input().split())
p=list(map(int,input().split()))
c=list(map(int,input().split()))
now=-100000000000
for i in range(1,n+1):
    retu=[]
    sum1=0
    sum2=0
    sum3=0
    max1=-1000000000000
    max2=-1000000000000
    flag=0
    x=i
    while(flag==0):
        x=p[x-1]
        sum1+=c[x-1]
        retu.append(c[x-1])
        if x==i:
            flag=1
    if len(retu)>=k: #操作回数が周期未満
        for j in range(k):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        ans=max1
    elif sum1<=0: #周期和が負
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        ans=max2
    else:
        w=k%len(retu)
        
        if w==0:
            w=len(retu)
        for j in range(w):
            sum2+=retu[j]
            if sum2>max1:
                max1=sum2
        for j in range(len(retu)):
            sum3+=retu[j]
            if sum3>max2:
                max2=sum3
        if max1+sum1>max2:
            ans=sum1*(k-w)//len(retu)+max1
        else:
            ans=sum1*((k-w)//len(retu)-1)+max2
    now=max(now,ans)
print(now)
import math
r = map(float, raw_input())
a = r * r * math.pi
b = 2 * r * math.pi
print "%f %f" % (a,b)

### FIXED CODE ###

import math
r = float(raw_input())
a = r ** 2 * math.pi
b = 2 * r * math.pi
print "%f %f" % (a,b)
n, m = map(int,input().split())
l = list(map(int,input().split()))

import math

def gcd(x, y):
    if y == 0:
        return x
    return gcd(y, x%y)
def lcm(x, y):
    return x * y / gcd(x, y)

ans = 1
tf = True

for i in l:
    i /= 2
    ans = lcm(ans, i)
    if (ans / i) % 2 == 0:
        print(0)
        tf = False
        break
    if ans > m:
        print(0)
        tf = False
        break

if tf == True:
    print(math.ceil(m / (ans * 2)))

### FIXED CODE ###

k, x = map(int, input().split())
print('Yes' if x <= 500 * k else 'No')
x=int(input())
y=int(input())
s=x*y
l=2*x+2*y
print(s," ",l)

### FIXED CODE ###

x,y=[int(i) for i in input().split()]
s=x*y
l=2*x+2*y
print(s,l)
a=int(input())
b=a%1000
if b==0 : c=0
else :c=1000-b n
print(str(c))

### FIXED CODE ###

a=int(input())
b=a%1000
if b==0 : c=0
else :c=1000-b
print(str(c))