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

import sys
from collections import deque
import copy
import math
def main():
    N, K = map(int, input().split())
    a = [float(i) for i in input().split()]
    A = [0] * (N + 1)
    if K >= 41:
        for i in range(1, N+1):
            if i > 1:
                print(' ', end='')
            print(N, end='')
        print()
        sys.exit()
    for i in range(1, N+1):
        A[i] = a[i - 1]
    for _ in range(K):
        count = [0] * (N + 1)
        for i in range(1, N+1):
            if A[i] >= N:
                left_d = 1
                right_d = N
            else:
                left_d = math.ceil(i - A[i] - 0.5)
                right_d =  math.floor(i + A[i] + 0.5)
                if left_d < 1:
                    left_d = 1
                if right_d > N:
                    right_d = N
            for j in range(left_d, right_d+1):
                count[j] += 1
        A = copy.copy(count)
        flag = True
        for i in range(1, N+1):
            if A[i] != N:
                flag = False
                break
        if flag:
            break
    for i in range(1, N+1):
        if i > 1:
            print(' ', end='')
        print(A[i], end='')
    print()


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
from collections import deque
import copy
import math
def main():
    N, K = map(int, input().split())
    a = [int(i) for i in input().split()]
    A = [0] * (N + 1)
    if K >= 41:
        for i in range(1, N+1):
            if i > 1:
                print(' ', end='')
            print(N, end='')
        print()
        sys.exit()
    for i in range(1, N+1):
        A[i] = a[i - 1]
    for _ in range(K):
        count = [0] * (N + 1)
        for i in range(1, N+1):
            left_d = max(math.ceil(i - A[i]), 1)
            right_d = min(math.floor(i + A[i]), N)
            count[left_d] += 1
            if right_d + 1 <= N:
                count[right_d + 1] -= 1
        for i in range(1, N+1):
            count[i] += count[i - 1]
        A = copy.copy(count)
    
    for i in range(1, N+1):
        if i > 1:
            print(' ', end='')
        print(A[i], end='')
    print()


if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
mod = int(1e9+7)
input=sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
#S[0]は使わない
S = [[0]*60 for _ in range(N)]

tmp = A[-1]
idx = 0
while tmp > 0:
    if tmp%2:
        S[-1][idx] += 1
    idx += 1
    tmp //= 2

for i in range(N-2, 0, -1):
    for j in range(60):
        S[i][j] = S[i+1][j]
    tmp = A[i]
    idx = 0
    while tmp > 0:
        if tmp % 2:
            S[i][idx] += 1
        idx += 1
        tmp //= 2

ans = 0
for i in range(N-1):
    tmp = A[i]
    idx = 0
    while tmp > 0:
        if tmp % 2:
            ans += ((N-1-i)-S[i+1][idx])*2**idx

        else:
            ans += S[i+1][idx]*2**idx

        idx += 1
        tmp //= 2

    rest = S[i+1][idx:]
    for x in rest:
        ans += x*2**idx
        idx += 1

    ans %= mod

print(ans)

### FIXED CODE ###

mod = int(1e9+7)
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(60):
    a = 0
    b = 0
    for j in range(N):
        if A[j] % 2:
            a += 1
        else:
            b += 1
        A[j] //= 2
    ans += a*b*pow(2, i, mod)
    ans %= mod
print(ans)
### BUGGY CODE ###

#自然数m以下の素数を返す
def prime_numbers(m):
    primes = set()
    n = 2
    while n <= m:
        if all(n % i != 0 for i in primes):
            primes.add(n)
        n += 1
    return primes

q = int(input())
l = []
r = []
for i in range(q):
    L,R = map(int,input().split())
    l.append(L)
    r.append(R)

p = prime_numbers(max(r))
cnt = [0 for i in range(max(r)+1)]
for i in p:
    if (i + 1) // 2 in p:
        cnt[i]+=1

for i in range(1,len(cnt)):
    cnt[i]+=cnt[i - 1]

for i in range(q):
    print(cnt[r[i]] - cnt[l[i] - 1])

### FIXED CODE ###

#素数列挙
def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

q = int(input())
l = []
r = []
for i in range(q):
    L,R = map(int,input().split())
    l.append(L)
    r.append(R)

p = primes(max(r))
cnt = [0 for i in range(max(r)+1)]
for i in p:
    if (i + 1) // 2 in p:
        cnt[i]+=1

for i in range(1,len(cnt)):
    cnt[i]+=cnt[i - 1]

for i in range(q):
    print(cnt[r[i]] - cnt[l[i] - 1])
### BUGGY CODE ###

def numerator(x):
    return x * (x + 1) // 2

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

ACC = 1
for p in P:
    ACC *= p

R = 0
for p in P[:K]:
    R += numerator(p) * ACC // p

max_value = R
for i in range(N-K):
    R += numerator(P[i+K]) * ACC // P[i+K] - numerator(P[i]) * ACC // P[i]
    max_value = max(max_value, R)
print(max_value / ACC)


### FIXED CODE ###

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

#期待値 : (p+1)//2
range_total = sum(P[:K]) + K
max_value = range_total
for i in range(N-K):
    range_total += P[K+i] - P[i]
    max_value = max(max_value, range_total)
print(max_value / 2)
### BUGGY CODE ###

s=input()
s=input().split()
t=input()
t=input().split()
s.sort()
t.sort()
f=False
for c in t:
 if not c in s:
  print("NO")
  f=True
  break
 else:
  s.remove(c)
if not f:
 print("YES")

### FIXED CODE ###

lena=int(input())
s=input().split()
a=[]
for i in s:
 a.append(int(i))
a.sort()
lenb=int(input())
s=input().split()
b=[]
for i in s:
 b.append(int(i))
b.sort()
i=0
j=0
while i<lena:
    if a[i]==b[j]:
        i+=1
        j+=1
        if j==lenb:
            break
    elif a[i]<b[j]:
        i+=1
    else:
        break
if j==lenb:
    print("YES")
else:
    print("NO")
### BUGGY CODE ###

def cmb(n, r, p):
    r = min(r, n - r)
    res = 1
    for i in range(r):
        res *= pow(i+1, p-2, p)  # 分母の逆元
        res *= (n-i) % p  # 分子
    return res % p

p = 10 ** 9 + 7
n, a, b = map(int, input().split())
ans = pow(2, n, p) - 1
ans -= cmb(n, a, p) + cmb(n, b, p)
print(ans % p)


### FIXED CODE ###

def cmb(n, r, p):
    r = min(r, n - r)
    res = 1
    for i in range(r):
        res *= pow(i+1, p-2, p)
        res *= n-i
        res %= p
    return res

p = 10 ** 9 + 7
n, a, b = map(int, input().split())
ans = pow(2, n, p) - 1
ans -= cmb(n, a, p) + cmb(n, b, p)
print(ans % p)
### BUGGY CODE ###

#097_C
s = input()
k = int(input())
ans = set()

for i in range(1, len(s)+1):
    for j in range(len(s)-i+1):
        ans.add(s[j:(i+j)])

print(sorted(list(ans))[k-1])

### FIXED CODE ###

#097_C
s = input()
k = int(input())
ans = set()

for i in range(1, min(k, len(s))+1):
    for j in range(len(s)-i+1):
        ans.add(s[j:(i+j)])

print(sorted(list(ans))[k-1])
### BUGGY CODE ###

S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)

### FIXED CODE ###

S = input()
Q = int(input())
#query = [input().split() for _ in range(Q)]

rev = False
forw = ""
back = ""
for i in range(Q):
  query = input().split()
  if query[0] == "1":
    rev = not rev
  else:
    if (not rev and query[1] == "1") or (rev and query[1] == "2"):
      forw = query[2] + forw
    else:
      back += query[2]
#S = forw + S + back
if rev:
  S = back[::-1] + S[::-1] + forw[::-1]
else:
  S = forw + S + back
print(S)
### BUGGY CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

a = inpl()
cnt = 0
while True:
    for i in a:
        if i%2:
            print(cnt)
            quit()
if a[0] == a[1] == a[2]:
    print(-1)
else:
    while True:
        for i in a:
            if i%2:
                print(cnt)
                quit()
        b = [0] * 3
        b[0] = (a[1] + a[2]) // 2
        b[1] = (a[0] + a[2]) // 2
        b[2] = (a[1] + a[0]) // 2
        cnt += 1
        a[::] = b[::]



### FIXED CODE ###

from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

a = inpl()
cnt = 0
for i in a:
    if i%2:
        print(cnt)
        quit()
if a[0] == a[1] == a[2]:
    print(-1)
else:
    while True:
        for i in a:
            if i%2:
                print(cnt)
                quit()
        b = [0] * 3
        b[0] = (a[1] + a[2]) // 2
        b[1] = (a[0] + a[2]) // 2
        b[2] = (a[1] + a[0]) // 2
        cnt += 1
        a[::] = b[::]
### BUGGY CODE ###

def lcs_length(a, b):
    table = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
    for i, ca in enumerate(a, 1):
        for j, cb in enumerate(b, 1):
            table[i][j] = (
                table[i - 1][j - 1] + 1 if ca == cb else
                max(table[i][j - 1], table[i - 1][j]))
    return table[-1][-1]

n=int(input())
s=list(input())
ans=[]
for i in range(1,n):
  s1=s[0:i];s2=s[i:n]
  ans.append(lcs_length(s1,s2))
print(max(ans))

### FIXED CODE ###

n=int(input())
s=input()
l=0;r=0;ans=0
while r<n:
  t=s[l:r]
  if t in s[r:n]:
    ans=max(ans,len(t))
    r+=1
  else:
    l+=1
print(ans)
### BUGGY CODE ###

def main(): 
    n = int(input())
    s = [sorted(input()) for _ in range(n)]
    
    cnt = 0
    for i in range(n):
        for j in range(i+1,n):
            if s[i] == s[j]:
                cnt += 1

    print(cnt)


if __name__ == "__main__":
    main() 

### FIXED CODE ###

def main(): 
    n = int(input())
    s = [''.join(sorted(input())) for _ in range(n)]
    ans = 0
    dic = {}
    for si in s:
        if si in dic:
            ans += dic[si]
            dic[si] += 1
        else:
            dic[si] = 1
    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

from math import factorial as fact
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(n,r))

def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1,x2):
	cf=[]
	m=min(x1,x2)
	for i in range(2,m+1):
		if x1 % i==0 and x2%i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)



A,B=input2()
ch=ch(A,B)
count=0
#素数を数える
for num in ch:
	flag=True
	for k in range(2, int(math.sqrt(num)) +1):
		if num%k==0:
			#素数ではない
			flag=False
			break
	if flag==True:
		count+=1
print(count+1)




### FIXED CODE ###

from math import factorial as fact
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1

def combinations(n,r):
	return list(itertools.combinations(n,r))

def all_sets(num_list):
	subsets=[]
	for i in range(2,len(num_list) + 1):
		for c in combinations(num_list, i):
			subsets.append(c) 
	return subsets

#公約数の列挙
def ch(x1):
	cf=[]
	for i in range(2,math.sqrt(x1)+1):
		if x1 % i==0:
			cf.append(i)
	return cf

def CountOneRoots(x,y):
	#x:縦，y:横
	return fact(x+y)/fact(x)/fact(y)

# 素因数分解
def factorization(n):
	tmp=n
	count=0
	for i in range(2,int(n**0.5)+2):
		if tmp%i==0:
			cnt=0
			while tmp%i==0:
				cnt+=1
				tmp//=i
			count+=1
	if tmp!=1:
		count+=1

	return count
			


A,B=input2()
gcd=fractions.gcd(A,B)
# ch=ch(gcd)
print(factorization(int(gcd))+1)
### BUGGY CODE ###

def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    


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

for i in range(n):
    if E.count(a[i]) == 0:
        E.append(a[i])

m = len(E)

num = [0] * m
comb = [0] * m
count = 0

for i in range(m):
    num[i] = a.count(E[i])
    comb[i] = C(num[i])
    count += comb[i]

for i in range(n):
    l = E.index(a[i])
    print(count - comb[l] + C(num[l]-1))

### FIXED CODE ###


from collections import Counter

def C(n):
    if n < 2: return 0
    return n * (n-1) // 2    
    
n = int(input())
a = list(map(int, input().split()))
c = Counter(a)

count = 0

for i, j in c.items():
    count += C(j)

for i in a:
    m = c[i]
    print(count - m + 1)
### BUGGY CODE ###

from collections import Counter
N = int(input())
A = list(map(int, input().split()))
counter = Counter(A)
exact_counter = {}
for v, n in counter.items():
    if n != 1:
        exact_counter[v] = n

for k in range(N):
    a = A[k]
    ans = 0
    for v, n in exact_counter.items():
        if a == v:
            n -= 1
        ans += n * (n - 1) // 2
    print(ans)


### FIXED CODE ###

from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
dict = defaultdict(int)
ans = 0
for k in range(N):
    a = A[k]
    ans += dict[a]
    dict[a] += 1

for k in range(N):
    a = A[k]
    print(ans - dict[a] + 1)
### BUGGY CODE ###

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

count = 0
for i in range(1, n+1):
  if i in set(P):
    count += 1
    P = P[:P.index(i)]
  if len(P) == 0:
    break

print(count)

### FIXED CODE ###

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

count = 0
tmpmin = P[0]
for p in P:
  if p <= tmpmin:
    count += 1
    tmpmin = p

print(count)
### BUGGY CODE ###

from functools import lru_cache
import sys

sys.setrecursionlimit(10**8)

N = int(input())
ABC = [list(map(int, input().split(" "))) for _ in range(N)]

# l = [[0 for _ in range(3)] for _ in range(N+10)]
@lru_cache(maxsize=None)
def dp(i, use):
    if i == -1:
        return 0
    max_num = 0
    for k in range(3):
        if k == use:
            continue
        max_num = max(max_num, dp(i-1, k)+ABC[i][use])
    return max_num

print(max(dp(N-1, m) for m in range(3)))


### FIXED CODE ###

from functools import lru_cache
import sys

sys.setrecursionlimit(10**8)

N = int(input())
ABC = [list(map(int, input().split(" "))) for _ in range(N)]

l = [[None for _ in range(3)] for _ in range(N+10)]
# @lru_cache(maxsize=None)
def dp(i, use):
    if i == -1:
        return 0
    if l[i][use] != None:
        return l[i][use]
    max_num = 0
    for k in range(3):
        if k == use:
            continue
        max_num = max(max_num, dp(i-1, k)+ABC[i][use])
    l[i][use] = max_num 
    return max_num

print(max(dp(N-1, m) for m in range(3)))
### BUGGY CODE ###

r,g,b,n=map(int, input().split())
ans=0
for i in range(3001):
  for j in range(3001):
    v=i*r + j*g
    #print((n-v)/b)
    if n>=v:
      if (n-v)%b == 0:
        ans+=1
print(ans)

### FIXED CODE ###

r,g,b,n=map(int, input().split())
ans=0
for i in range(3001):
  for j in range(3001):
    v=i*r + j*g
    #print((n-v)/b)
    if n>=v:
      if (n-v)%b == 0:
        ans+=1
print(ans)
### BUGGY CODE ###

import bisect
N, M = map(int, input().split())
A = list(map(int, input().split()))

A.sort()
tmp = A.pop(-1)
index = 0
for _ in range(M):
  tmp //= 2
  index = bisect.bisect_left(A,tmp)
  if index < N - 1:
    A.insert(index, tmp)
    tmp = A.pop(-1)
A.append(tmp)

print(sum(A))

### FIXED CODE ###

import heapq

def minus_int(s):
  return -1 * int(s)

N, M = map(int, input().split())
A = list(map(minus_int, input().split()))
heapq.heapify(A)
for _ in range(M):
  tmp = int(heapq.heappop(A) / 2)
  heapq.heappush(A, tmp)

print(sum(A) * (-1))
### BUGGY CODE ###

# /usr/bin/python
# -*- coding: utf-8 -*-
import math



def main():
  N,M = map(int, input().split())
  route1 = []
  routeN = []
  for _ in range(M):
    ab = sorted(list(map(int, input().split())))
    if ab[0] == 1:
      route1.append(ab[1])
    elif ab[1] == N:
      routeN.append(ab[0])

  for i in route1:
    if i in routeN:
      return True
  return False

if __name__ == "__main__":
  if main():
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')

### FIXED CODE ###

# /usr/bin/python
# -*- coding: utf-8 -*-
import math



def main():
  N,M = map(int, input().split())
  route = [0 for _ in range(N+1)]

  for _ in range(M):
    a,b = map(int, input().split())
    if a == 1:
      route[b] += 1
    elif b == N:
      route[a] += 1

  if max(route) >= 2:
    return True
  else:
    return False


if __name__ == "__main__":
  if main():
    print('POSSIBLE')
  else:
    print('IMPOSSIBLE')
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

#output
import fractions, functools

mod = 10**9 + 7

def lcm_base(x, y):
    return (x*y) // fractions.gcd(x, y)
def lcm_list(numbers):
    return functools.reduce(lcm_base, numbers, 1)

lcm = lcm_list(A)

answer = 0
for i in range(N):
    answer += lcm//A[i]

print(answer % mod)

### FIXED CODE ###

import sys
input = sys.stdin.readline
 
#input
N = int(input())
A = list(map(int, input().split()))
 
#output
import fractions, functools
 
def lcm_base(x, y):
    return (x*y) // fractions.gcd(x, y)
def lcm_list(numbers):
    return functools.reduce(lcm_base, numbers, 1)
 
lcm = lcm_list(A)
 
answer = 0
for i in range(N):
    answer += lcm//A[i]
 
print(answer % (10**9 + 7))
### BUGGY CODE ###

N = int(input())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
M = int(input())
UV = [list(map(int,input().split())) for _ in [0]*M]

E = [[] for _ in [0]*N]
for a,b in AB:
    a-=1
    b-=1
    E[a].append(b)
    E[b].append(a)

P = [-1]*N
P[0] = 0
q = [0]
while q:
    i = q.pop()
    for j in E[i]:
        if P[j]!=-1: continue
        P[j] = i
        q.append(j)
P[0] = -1

def dist_bfs(N,E,start):
    d = [-1]*N
    d[start] = 0
    q = [start]
    while q:
        qq = []
        for i in q:
            di = d[i]
            for j in E[i]:
                if d[j]!=-1:continue
                d[j] = di+1
                q.append(j)
        q = qq
    return d

path = []
for u,v in UV:
    u -= 1
    v -= 1
    tmp = []
    d = dist_bfs(N,E,u)
    q = [v]
    while q:
        i = q.pop()
        di = d[i]
        for j in E[i]:
            if d[j] == di-1:
                q.append(j)
                if P[i]==j:
                    tmp.append(i)
                else:
                    tmp.append(j)
    path.append(set(tmp))

ans = 2**(N-1)
for i in range(1,2**M):
    tmp = set()
    cnt = 0
    for j in range(M):
        if (i>>j)&1==0:continue
        cnt += 1
        tmp = tmp | path[j]
    ans += ((-1)**cnt) * (2**(N-1-len(tmp)))
print(ans)

### FIXED CODE ###

N = int(input())
AB = [list(map(int,input().split())) for _ in [0]*(N-1)]
M = int(input())
UV = [list(map(int,input().split())) for _ in [0]*M]

E = [[] for _ in [0]*N]
for a,b in AB:
    a-=1
    b-=1
    E[a].append(b)
    E[b].append(a)

P = [-1]*N
P[0] = 0
q = [0]
while q:
    i = q.pop()
    for j in E[i]:
        if P[j]!=-1: continue
        P[j] = i
        q.append(j)
P[0] = -1

def dist_bfs(N,E,start):
    d = [-1]*N
    d[start] = 0
    q = [start]
    while q:
        qq = []
        for i in q:
            di = d[i]
            for j in E[i]:
                if d[j]!=-1:continue
                d[j] = di+1
                q.append(j)
        q = qq
    return d

path = []
for u,v in UV:
    u -= 1
    v -= 1
    tmp = []
    d = dist_bfs(N,E,u)
    q = [v]
    while q:
        i = q.pop()
        di = d[i]
        for j in E[i]:
            if d[j] == di-1:
                q.append(j)
                if P[i]==j:
                    tmp.append(i)
                else:
                    tmp.append(j)
    path.append(set(tmp))

ans = 2**(N-1)
for i in range(1,2**M):
    tmp = set(range(1,N))
    cnt = 0
    for j in range(M):
        if (i>>j)&1==0:continue
        cnt += 1
        tmp -= path[j]
    ans += ((-1)**cnt) * (2**(len(tmp)))
print(ans)
### BUGGY CODE ###

X = int(input())
x = int(X**0.5)
num = []
for b in range(1,x+1) :
    p = 2
    while b**p <= X :
        p += 1
    num.append(b**(p-1))
print(max(num))

### FIXED CODE ###

X = int(input())
x = int(X**0.5)
num = []
if X <= 3 :
    print(1)
else :
    for b in range(2,x+1) :
        p = 2
        while b**p <= X :
            p += 1
        num.append(b**(p-1))
    print(max(num))
### BUGGY CODE ###

s = str(input())
q = int(input())
usd = False

for _ in range(q):
    t, *fc = input().split()
    t = int(t)
    if fc:
        fc[0] = int(fc[0])

    if t == 1:
        usd = not usd
        continue
    if t == 2:
        if (fc[0] == 1 and not usd) or (fc[0] == 2 and usd):
            s = fc[1] + s
        else:
            s = s + fc[1]

if usd:
    s = s[::-1]
print(s)

### FIXED CODE ###

# TLE 乙
from collections import deque


s = input()
q = int(input())
usd = False
d = deque(s)

for _ in range(q):
    t, *fc = input().split()
    t = int(t)
    if fc:
        fc[0] = int(fc[0])

    if t == 1:
        usd = not usd
        continue
    if t == 2:
        if (fc[0] == 1 and not usd) or (fc[0] == 2 and usd):
            d.appendleft(fc[1])
        else:
            d.append(fc[1])

s = ''.join(list(d))
if usd:
    s = s[::-1]
print(s)
### BUGGY CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

mv_array = [[] for _ in range(n)]
score_mv_array = [[] for _ in range(n)]
score_array = [0]*n

def get_sum(array):
    score_array = [0]
    for i, a in enumerate(array):
        score_array.append(score_array[i]+a)
    return max(score_array)

def get_max(array, q, mod):
    if q == 0:
        m_score = get_sum(array[:mod])
    else:
        m_score = get_sum(array+array[:mod])
        m_score = sum(array)*(q-1) + m_score
    return m_score


for i in range(n):
    score = 0
    max_score = c[i]
    pos = i
    for j in range(k):
        pos = p[pos]-1
        if pos in mv_array[i]:
            len_mv = len(score_mv_array[i])
            q, mod = divmod(k-j, len_mv)
            score += get_max(score_mv_array[i], q, mod)
            max_score = max(score, max_score)
            break
        score += c[pos]
        mv_array[i].append(pos)
        score_mv_array[i].append(c[pos])
        max_score = max(score, max_score)
    score_array[i] = max_score

print(max(score_array))

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))

max_score = -float('inf')

for i in range(n):
    loop_score_array = []
    loop_score = 0
    loop_len = 0
    pos = i
    while True:
        pos = p[pos] - 1
        loop_score += c[pos]
        loop_score_array.append(loop_score)
        loop_len+=1
        if pos == i:
            break
    for i, score in enumerate(loop_score_array):
        if k-i-1 < 0:
            continue
        loop_num = (k-i-1)//loop_len
        score_sum = score + max(0, loop_score)*loop_num
        max_score = max(score_sum, max_score)
print(max_score)