Harryxun/stf_run huggingface dataset:
B

�����j\_������������������@���s����d�dl�Z�d�dlmZ�eee������\ZZdd��e�����D��Z	e
e��e	��Ze
e�ekr`ed���nJee���ed�d�Zd�Zx(ee
e�e��D�]Zeee�d�7�Zq�W�ee��dS�)�����N)�
itemgetterc�������������C���s���g�|�]}t�|��qS���)�int)�.0�jr���r����
ABC081C.py�
<listcomp>���s����r�������)�key)�collections�operatorr����mapr����input�split�N�KZball�dict�Counter�kind�len�print�sorted�itemsZskindZsumNum�range�ir���r���r���r����<module>���s���


### FIXED CODE ###

import collections
from operator import itemgetter
N,K = map(int,input().split())
ball = [int(j) for j in input().split()]

kind = dict(collections.Counter(ball))

if len(kind) <= K:
    print(0)
else:
    skind = sorted(kind.items(),key=itemgetter(1))
    sumNum = 0
    for i in range(len(skind)-K):
        sumNum += skind[i][1]
    print(sumNum)
while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            P.append((lock, 1 << int(c)))
            lock |= 1 << int(c)
    Q = set()
    i = 0
    while i < len(P):
        lock1, t1 = P[i]
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
        i += 1
    ok = 1
    for lock1, c1 in P:
        for lock2, c2 in P:
            if lock1 & lock2 == 0 and c1 & lock2 == c1 and c2 & lock1 == c2:
                ok = 0
    for lock, c in P:
        if lock & c == c:
            ok = 0
    print "SAFE"*ok or "UNSAFE"

### FIXED CODE ###

while 1:
    n = input()
    if n == 0:
        break
    s = raw_input()
    Q = set()
    lock = 0
    P = []
    for c in s:
        if c is 'u':
            lock = 0
        else:
            v = (lock, 1 << int(c))
            if v not in Q:
                P.append(v)
                Q.add(v)
            lock |= 1 << int(c)
    i = 0; ok = 1
    while i < len(P) and ok:
        lock1, t1 = P[i]
        if lock1 & t1 == t1:
            ok = 0
        for j in xrange(i):
            lock2, t2 = P[j]
            if lock1 & lock2 == 0 and t1 & lock2 == t1:
                if t2 & lock1 == t2:
                    ok = 0
                v = (lock1 | lock2, t2)
                if v not in Q:
                    P.append(v)
                    Q.add(v)
        i += 1
    print "SAFE"*ok or "UNSAFE"
s = input()
t = set(s)
if len(t) != 2:
  print('No')
else:
  for char in t:
    ans = arr([(s.count(char) == 2) for char in t])
  if ans:
    print('Yes')
  else:
    print('No')

### FIXED CODE ###

s = input()
s = sorted(s)
if s[0] == s[1] and s[2] == s[3] and not (s[1] == s[2]):
  print('Yes')
else:
  print('No')
def main():
    mod = 1000000007
    n, k = (int(x) for x in input().split())
    l = (i for i in range(n + 1))  # おまけの小さい部分，でも大事

    # 累積和を計算しておく，前からと後ろから。
    former = 0
    acm = tuple((former:= former + i) for i in range(n + 1))
    former = 0
    acm_rev = tuple((former:= former + i) for i in range(n, -1, -1))

    ans = 0
    # 選ぶ数の数で分ける。でかい部分は無視して，
    # 最小値と最大値が分かれば取りうる値も分かる。
    for j in range(k-1, n + 1):
        ans += acm_rev[j] - acm[j] + 1
        ans %= mod
    return ans


if __name__ == '__main__':
    print(main())


### FIXED CODE ###

def main():
    mod = 1000000007
    n, k = (int(x) for x in input().split())
    l = (i for i in range(n + 1))  # おまけの小さい部分，でも大事

    # 累積和を計算しておく，前からと後ろから。
    former = 0
    acm = tuple((former:= former + i) for i in range(n + 1))
    former = 0
    acm_rev = tuple((former:= former + i) for i in range(n, -1, -1))

    ans = 0
    # 選ぶ数の数で分ける。でかい部分は無視して，
    # 最小値と最大値が分かれば取りうる値も分かる。
    for j in range(k-1, n + 1):
        ans += acm_rev[j] - acm[j] + 1
        ans %= mod
    return ans


if __name__ == '__main__':
    print(main())
A = map(int, input().split())
print(max(A) min(A))

### FIXED CODE ###

a = list(map(int, input().split()))
print(abs(max(a) - min(a)))
from functools import lru_cache

MOD = pow(10, 9) + 7

H, W = map(int, input().split())
A = [input() for _ in range(H)]

@lru_cache(maxsize=H*W)
def calc(h, w):
    if h == H -1 and w == W -1:
        return 1
    count = 0
    # right
    if w + 1 < W  and A[h][w + 1] == ".":
        count += calc(h, w + 1)
        count %= MOD

    if h + 1 < H  and A[h + 1][w] == ".":
        count += calc(h + 1, w)
        count %= MOD

    return count % MOD

for w in range(W):
    for h in range(H):
        calc(h, w)
print(calc(0,0))


### FIXED CODE ###

from functools import lru_cache

MOD = pow(10, 9) + 7

H, W = map(int, input().split())
A = [input() for _ in range(H)]
dp = [ [0 for j in range(W + 1)] for i in range(H + 1)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if w == 1 and h == 1:
            dp[1][1] = 1
            continue
 
        if A[h-1][w-1] == '.':
            dp[h][w] = (dp[h-1][w] + dp[h][w-1]) % MOD
            continue
        if A[h-1][w-1] == '#':
            dp[h][w]  = 0



print(dp[H][W])
S = list(input())
T = list(input())

# S=list("BBAAABA")
# T=list("BBBA")

q = int(input())

l = []
for i in range(q):
	l.append(list(map(int,input().split(" "))))


sta = [0 for i in range(len(S))]
stb = [0 for i in range(len(S))]

tta = [0 for i in range(len(T))]
tta = [0 for i in range(len(T))]

if S[0] == "A":
	sta[0] = 1
else:
	stb[0] = 1

if T[0] == "A":
	tta[0] = 1
else:
	ttb[0] = 1

for i in range(1,len(S)):
	if S[i] == "A":
		sta[i] = sta[i-1]+1
	else:
		stb[i] = stb[i-1]+1

for i in range(1,len(T)):
	if T[i] == "A":
		tta[i] = tta[i-1]+1
	else:
		ttb[i] = ttb[i-1]+1

def get(l,a,b):
	if a == 0:
		return(l[b])
	else:
		return(l[b]-l[a-1])
		
for a,b,c,d in l:
	sa = get(sta,a-1,b-1)
	sb = get(stb,a-1,b-1)
	ta = get(tta,c-1,d-1)
	tb = get(ttb,c-1,d-1)


	if (sa+sb*2)%3 == (ta+tb*2)%3:
		print("YES")
	else:
		print("NO")





















### FIXED CODE ###

S = list(input())
T = list(input())

# S=list("BBAAABA")
# T=list("BBBA")

q = int(input())

l = []
for i in range(q):
	l.append(list(map(int,input().split(" "))))


sta = [0 for i in range(len(S))]
stb = [0 for i in range(len(S))]

tta = [0 for i in range(len(T))]
ttb = [0 for i in range(len(T))]

if S[0] == "A":
	sta[0] = 1
else:
	stb[0] = 1

if T[0] == "A":
	tta[0] = 1
else:
	ttb[0] = 1

for i in range(1,len(S)):
	if S[i] == "A":
		sta[i] = sta[i-1]+1
		stb[i] = stb[i-1]
	else:
		sta[i] = sta[i-1]
		stb[i] = stb[i-1]+1

for i in range(1,len(T)):
	if T[i] == "A":
		tta[i] = tta[i-1]+1
		ttb[i] = ttb[i-1]
	else:
		tta[i] = tta[i-1]
		ttb[i] = ttb[i-1]+1


def get(l,a,b):
	if a == 0:
		return(l[b])
	else:
		return(l[b]-l[a-1])

for a,b,c,d in l:
	sa = get(sta,a-1,b-1)
	sb = get(stb,a-1,b-1)
	ta = get(tta,c-1,d-1)
	tb = get(ttb,c-1,d-1)


	if (sa+sb*2)%3 == (ta+tb*2)%3:
		print("YES")
	else:
		print("NO")
def mergesort(A, left, right):

    if left + 1 < right:
        mid = (left + right)/2
        
        mergesort(A, left, mid)
        mergesort(A, mid, right)
        merge(A, left, mid, right)


def merge(A, left, mid, right):
   
    n1 = mid -left
    n2 = right - mid
    L = []
    R = []
    x = [" "," ", 100000000000000000000000000000000]
    for i in range(0, n1):
        L.append(A[left + i])
    for i in range(0, n2):
        R.append(A[mid + i])
    L.append(x)
    R.append(x)
    i = 0
    j = 0
    for k in range(left, right):
        if int(L[i][-1]) <= int(R[j][-1]):
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
        



# kannsei
def partition(A, p, r):
    x = int(A[r-1][2])
    i = p - 1
    for j in range(p, r-1):
        if int(A[j][2]) <= x:
            i = i + 1
            A[i],A[j]=A[j], A[i]
            
    A[i+1], A[r-1]=A[r-1], A[i+1]
    
    retu
rn i+1

def quicksort(A, p, r):
    if p < r-1:
        q = partition(A, p, r)
        
        quicksort(A, p, q)
        quicksort(A, q+1, r)


if __name__ == "__main__":
    num = int(raw_input())
    num_list = []
    num_list1 = []
    for i in range(num):
        num_list.append(raw_input())
    num_list1 = list(num_list)
    quicksort(num_list, 0, len(num_list))
    print " \n".join(num_list)
    mergesort(num_list1, 0, len(num_list1))
    
    if num_list == num_list1:
        print ("Stable")
    else:c
        print ("Not stable")
print "\n".join(num_list1)
    

### FIXED CODE ###

def mergesort(A, left, right):
 
    if left + 1 < right:
        mid = (left + right)/2
         
        mergesort(A, left, mid)
        mergesort(A, mid, right)
        merge(A, left, mid, right)
 
 
def merge(A, left, mid, right):

    n1 = mid -left
    n2 = right - mid
    L = []
    R = []
    x = "  10000000000"
    for i in range(0, n1):
        L.append(A[left + i])
    for i in range(0, n2):
        R.append(A[mid + i])
    L.append(x)
    R.append(x)
    i = 0
    j = 0
    for k in range(left, right):
        if int(L[i][2:]) <= int(R[j][2:]):
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
            
            
def partition(A, p, r):
    x = int(A[r-1][2:])
    i = p - 1
    for j in range(p, r-1):
        if int(A[j][2:]) <= x:
            i = i + 1
            A[i],A[j]=A[j], A[i]
             
    A[i+1], A[r-1]=A[r-1], A[i+1]
    return i+1
 
def quicksort(A, p, r):
    if p < r-1:
        q = partition(A, p, r)
         
        quicksort(A, p, q)
        quicksort(A, q+1, r)
 
 
 
 
if __name__ == "__main__":
    num = int(raw_input())
    num_list = []
    num_list1 = []
    for i in range(num):
        num_list.append(raw_input())
    num_list1 = list(num_list)
    quicksort(num_list, 0, len(num_list))
  
    mergesort(num_list1, 0, len(num_list1))
     
    if num_list == num_list1:
        print ("Stable")
    else:
        print ("Not stable")
    print "\n".join(num_list)
import math
A,B,C,D = list(map(int, input().split()))
a=(A-1)//C
b=B//C
c=(A-1)//D
d=B//D
g = C*D//math.gcd(C,D)
e=(A-1)//(g)
f=B//(g)

print(B-A+1 - (b-a) - (d-c) + (f-e))

### FIXED CODE ###

import fractions
A,B,C,D = list(map(int, input().split()))
a=(A-1)//C
b=B//C
c=(A-1)//D
d=B//D
g = C*D//fractions.gcd(C,D)
e=(A-1)//(g)
f=B//(g)

print(B-A+1 - (b-a) - (d-c) + (f-e))
S = input()
if S=="ABC":
    print("ARC")
elsse:
    print("ABC")

### FIXED CODE ###

S = input()
if S=="ABC":
    print("ARC")
else:
    print("ABC")
N = int(input())
a = list(map(int,input().split(" ")))

memo = [ [-1]*N  for _ in range(N) ]


def dfs(l,r,turn):
    if l == r:
        return 0

    if memo[l][r-1] != -1:
        return memo[l][r-1]

    if turn % 2 == 0:
        ret = max(a[l] + dfs(l+1,r,(turn+1)%2),a[r-1] + dfs(l,r-1,(turn+1)%2))
    else:
        ret = min(-a[l] + dfs(l+1,r,(turn+1)%2),-a[r-1] + dfs(l,r-1,(turn+1)%2))

    memo[l][r-1] = ret
    return memo[l][r-1]

ans = dfs(0,N,0)
print(ans)




### FIXED CODE ###

import sys
N = int(input())
a = list(map(int,input().split(" ")))

dp = [ [0]*N  for _ in range(N) ]

for l in range(N-1,-1,-1):
    for r in range(l,N):
        turn = ((N - r) + l + 1) % 2

        if turn == 0:
            if r > 0 and l < N-1:
                dp[l][r] = max(a[l] + dp[l+1][r],a[r]+dp[l][r-1])
            elif l < N-1:
                dp[l][r] =  a[l] + dp[l+1][r]
            else:
                dp[l][r] =  a[r] + dp[l][r-1]
        else:
            if r > 0 and l < N-1:
                dp[l][r] = min(-a[l] + dp[l+1][r],-a[r]+dp[l][r-1])
            elif l < N-1:
                dp[l][r] = -a[l] + dp[l+1][r]
            else:
                dp[l][r] = -a[r] + dp[l][r-1]
print(dp[0][N-1])
a,b,n = map(int, input().split())
M = [0]*(n+1)
for i in range(n+1):
    score = int(a*i/b) - a*int(i/b)
    M[i] = score

print(max(M))


### FIXED CODE ###

a,b,n = map(int, input().split())
m = min(n,b-1)
print(int(a*m/b)-a*int(m/b))
import sys

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

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N) + 1
    X[index-1] = S[i]

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

### FIXED CODE ###

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

def gcd(a, b):
    while b :
        a, b = b, a % b
    return a

def lcm(a, b):
    return a * b // gcd(a, b)

L = lcm(N, M)
s = L // M
t = L // N

if S[::s] != T[::t]:
    L = -1
print(L)
from math import gcd

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def solve():
  A, B = map(int, input().split())
  return len(set(prime_factorize(gcd(A, B)))) + 1

print(solve())

### FIXED CODE ###

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

def solve():
  A, B = map(int, input().split())
  return len(set(prime_factorize(gcd(A, B)))) + 1

print(solve())
a = -1
n,m = map(int, input().split())
s_and_c = [tuple(map(int,input().split())) for i in range(m)]
l = [0]*m
for t in s_and_c:
  l[t[0]-1] = t[1]
if len(l) == n:
  a = "".join([str(i) for i in l])
else:
  pass
print(a)

### FIXED CODE ###

n,m = map(int,input().split())
x = [tuple(map(int,input().split())) for i in range(m)]
f=True
for i in range(0,1001):
    i = str(i)
    if len(i)==n:
        if all([i[t[0]-1]==str(t[1]) for t in x]):
            print(i)
            f=False
            break
if f:
    print(-1)
import numpy as np

mod = 10**9 + 7

r1, c1, r2, c2 = map(int, input().split())
f = np.ones((r2 + 1, c2 + 1), dtype=np.int)
g = np.ones((r2 + 1, c2 + 1), dtype=np.int)
for i, j in np.ndindex(r2, c2):
  f[i+1, j+1] = (f[i, j+1] + f[i+1, j]) % mod
print(f[r1:, c1:].sum() % mod)

### FIXED CODE ###

mod = 10**9 + 7

r1, c1, r2, c2 = map(int, input().split())

fact = [1] * (r2 + c2 + 3)
inv = [1] * (r2 + c2 + 3)
inv_fact = [1] * (r2 + c2 + 3)
for i in range(2, r2 + c2 + 3):
  fact[i] = fact[i-1] * i % mod
  inv[i] = -inv[mod % i] * (mod // i) % mod
  inv_fact[i] = inv_fact[i-1] * inv[i] % mod

total = fact[r2 + c2 + 2] * inv_fact[r2 + 1] * inv_fact[c2 + 1] % mod
total -= fact[r1 + c2 + 1] * inv_fact[r1] * inv_fact[c2 + 1] % mod
total -= fact[r2 + c1 + 1] * inv_fact[c1] * inv_fact[r2 + 1] % mod
total += fact[r1 + c1] * inv_fact[r1] * inv_fact[c1] % mod

print(total % mod)
#! /usr/bin/env python3

# from fractions import gcd
from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

S = input()

c = Counter(S)

for i in c:
    if c[i] % 2 != 0:
        print("No")
        exit()
print("Yes")


### FIXED CODE ###

#! /usr/bin/env python3

from fractions import gcd
# from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

S = input()

c = Counter(S)

for i in c:
    if c[i] % 2 != 0:
        print("No")
        exit()
print("Yes")
from math import gcd
K = int(iniput())
sum_gcd = [0]*201
ans = 0

for i in range(1,K+1):
  for j in range(1,K+1):
    sum_gcd[gcd(i,j)] += 1
for i in range(1,K+1):
  for k in range(1,K+1):
    ans += GCD[i]*gcd(i,k)

print(ans)

### FIXED CODE ###

from math import gcd
K = int(input())
sum_gcd = [0]*201
ans = 0
 
for i in range(1,K+1):
  for j in range(1,K+1):
    sum_gcd[gcd(i,j)] += 1
for i in range(1,K+1):
  for k in range(1,K+1):
    ans += sum_gcd[i]*gcd(i,k)

print(ans)
N, K = map(int, input().split())
V = list(map(int, input().split()))

import copy

import sys

INT_MIN = -1 * sys.maxsize

import bisect

def dfs(k, v_q, v_plus, v_minus):
    v_p_sum = sum(v_plus)
    #if k == 0:
    #    return v_p_sum + sum(v_minus)
    #c = INT_MIN
    if k < len(v_minus):
        #v_minus = sorted(v_minus)
        c = v_p_sum + sum(v_minus[k:])
        return c
    
    
    v_q_left = copy.copy(v_q)
    l = v_q_left.popleft()
    if l >= 0:
        a = dfs(k-1, v_q_left, v_plus[:]+[l], v_minus[:])
    else:
        tmp = v_minus[:]
        bisect.insort(tmp, l)
        a = dfs(k-1, v_q_left, v_plus[:], tmp)
    
    v_q_right = copy.copy(v_q)
    r = v_q_right.pop()
    if r >= 0:
        b = dfs(k-1, v_q_right, v_plus[:]+[r], v_minus[:])
    else:
        tmp = v_minus[:]
        bisect.insort(tmp, r)
        b = dfs(k-1, v_q_right, v_plus[:], tmp)
    
    return max([a, b, v_p_sum])
    


from collections import deque
v_q = deque(V)

ans = dfs(K, v_q, [], [])
print(ans)

    
    

### FIXED CODE ###

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

# N < Kの場合があることに注意


INT_MIN = -1 * (10**10)

ans = INT_MIN

NK = min(N, K)
for left in range(0,NK+1):
    for right in range(0, NK+1-left):
        #print("========")
        #print(left, right)
        #print(V[:left], V[left:N-right], V[N-right:])
        
        rest_ope = K-left-right
        #print(rest_ope)
        my_v = V[:left]+V[N-right:]
        sum_v = 0
        for i, x in enumerate(sorted(my_v)):
            if x < 0 and i < rest_ope:
                continue
            else:
                sum_v += x
        #print(sum_v)
        ans = max(ans, sum_v)
        
print(ans)
N, K = map(int,input().split())
H = list(map(int,input().split()))

H.sort(reverse=True)
del H[:K]
H_sum = sum(H)
print(H_sum)


### FIXED CODE ###

H, A = map(int,input().split())

ans = 0

while H>0:
	ans += 1
	H -= A

print(ans)
import collections
q = collections.deque(maxlen=1000000)

n = int(input())
for i in range(n):
    com = input().split()
    if com[0]=='insert':
        q.appendleft(int(com[1]))
    elif com[0]=='delete':
        try:
            q.remove(int(com[0]))
        except:
            pass
    elif com[0]=='deleteFirst':
        q.popleft()
    elif com[0]=='deleteLast':
        q.pop()
print(' '.join(q))

### FIXED CODE ###

import collections
q = collections.deque()
n = int(input())
for i in range(n):
    com = input().split()
    if com[0] == 'insert':
        q.appendleft(int(com[1]))
    elif com[0] == 'delete':
        try:
            q.remove(int(com[1]))
        except:
            pass
    elif com[0] == 'deleteFirst':
        q.popleft()
    elif com[0] == 'deleteLast':
        q.pop()
print(*q)
x = input().split()
z=x[0]*y[1]
a=(x[0]+x[1])*2
print(z,a)

### FIXED CODE ###

x = map(int,raw_input().split())
z=x[0]*x[1]
a=(x[0]+x[1])*2
print z,a
n, k = map(int, input().split())
pos = [int(i) for i in input().split()]
score = [int(i) for i in input().split()]
if max(score) <= 0:
  print(max(score))
else:
  pos = [0] + pos
  score = [0] + score
  max_point = -10 ** 10
  s_l = [[0]]
  nei = {}
  dic = {}
  loop = 0
  for i in range(1, n + 1):
    f_pos = i
    #print('f_pos',f_pos)
    if f_pos not in nei:
      points = [0]
      count = 0
      loop += 1
      while True:
        n_pos = pos[f_pos]
        points.append(points[-1] + score[n_pos])
        count += 1
        nei[f_pos] = [count, loop]
        if n_pos in nei:
          dic[loop] = count
          break
        f_pos = n_pos
      #print(points)
      #print(nei)
      s_l.append(points)
      count = 1
    else:
      loop = nei[f_pos][1]
      count = nei[f_pos][0]
      points = s_l[loop]
    loop_num = dic[loop]
    s = points[count - 1]
    m = points[loop_num] - s
    a = k // loop_num
    b = k % loop_num
    if points[loop_num] > 0:
      point = points[loop_num] * a
      if point > max_point:
        max_point = point
    else:
      point = 0
      if a > 0:
        b = loop_num
    if b > 0:
      t_max = -10 ** 10
      if loop_num - count + 1 >= b:
        for j in range(b):
          t = points[j + count] - s
          if t > t_max:
            t_max = t
      else:
        for j in range(count, loop_num + 1):
          t = points[j] - s
          if t > t_max:
            t_max = t
        if b != loop_num and count != 1:
          for j in range(1, b - loop_num + count):
            t = points[j] + m
            if t > t_max:
              t_max = t
      point += t_max
    #print(point)
    if point > max_point:
      max_point = point
  print(max_point)

### FIXED CODE ###

n, k = map(int, input().split())
positions = [int(i) for i in input().split()]
scores = [int(i) for i in input().split()]
if max(scores) <= 0:
  print(max(scores))
else:
  positions = [0] + positions
  scores = [0] + scores
  max_point = -pow(10, 9)
  #print(max_point)
  for i in range(1, n + 1):
    s = []
    f_pos = positions[i]
    score = scores[f_pos]
    s.append(score)
    while f_pos != i:
      f_pos = positions[f_pos]
      score += scores[f_pos]
      s.append(score)
    loop = len(s)
    if loop >= k:
      point = max(s[:k])
    elif s[-1] <= 0:
      point = max(s)
    else:
      loop_c = k // loop
      r = k % loop
      point1 = s[-1] * loop_c
      if r != 0:
        point1 += max(s[:r])
      point2 = s[-1] * (loop_c - 1)
      point2 += max(0, max(s))
      point = max(point1, point2)
    max_point = max(max_point, point)
  print(max_point)
#input
N, K = map(int, input().split())
A = list(map(int, input().split()))

#output
#A[i]の電球は i-A[i] から i+A[i]までを照らす。
import numpy as np  

for _ in range(42):
    dp = np.zeros(N, int)
    for i in range(N):
        p = max(0, i-A[i])
        dp[p] += 1
        if i+A[i]+1 <= N-1:
            dp[i+A[i]+1] -= 1
    A = np.cumsum(dp)

if K > 41:
  print(np.array([N]*N, int)
else:
  print(np.cumsum(dp))


### FIXED CODE ###

#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #C
    #input
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
     
    #output
    #A[i]の電球は i-A[i] から i+A[i]までを照らす。
    import itertools
    K = min(K, 42)
    for _ in range(K):
        dp = [0]*N
        for i in range(N):
            p = max(0, i-A[i])
            dp[p] += 1
            if i+A[i]+1 <= N-1:
                dp[i+A[i]+1] -= 1
        dp = list(itertools.accumulate(dp))
        A = dp
     
    print(" ".join(repr(d) for d in dp))

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()
if input()="1":
  print("Hello World")
else:
  a=int(input())
  b=int(input())
  print(a+b)
  

### FIXED CODE ###

if input()=="1":
  print("Hello World")
else:
  a=int(input())
  b=int(input())
  print(a+b)