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) N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(x)-1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x)-1 int(x) int x 1 P_L = [int(x)-1 for x in input().split()] P_L [int(x)-1 for x in input().split()] int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C_L = [int(x) for x in input().split()] C_L [int(x) for x in input().split()] ans = float("-Inf") ans float("-Inf") 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) s range(N) range N _s_l = [] _s_l [] _v = s _v s _cumsum = 0 _cumsum 0 _ans = float("-Inf") _ans float("-Inf") float "-Inf" while True:
        _nextv = P_L[_v]
        _cumsum += C_L[_nextv]
        _s_l.append(_cumsum)
        _v = _nextv
        if _nextv == s:
            break
     True _nextv = P_L[_v] _nextv P_L[_v] P_L _v _cumsum += C_L[_nextv] _cumsum C_L[_nextv] C_L _nextv _s_l.append(_cumsum) _s_l.append _s_l append _cumsum _v = _nextv _v _nextv if _nextv == s:
            break
     _nextv == s _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))
     K <= len(_s_l) K len(_s_l) len _s_l _ans = max(_s_l[:K]) _ans max(_s_l[:K]) max _s_l[:K] _s_l :K K 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))
     _s_l[-1] < 0 _s_l[-1] _s_l -1 0 _ans = max(_s_l) _ans max(_s_l) max _s_l _l = len(_s_l) _l len(_s_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))
     K % _l == 0 K % _l K _l 0 _ans = max(_s_l[-1] * (K // _l),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) _ans max(_s_l[-1] * (K // _l),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) max _s_l[-1] * (K // _l) _s_l[-1] _s_l -1 K // _l K _l _s_l[-1] * (K // _l - 1) + max(_s_l) _s_l[-1] * (K // _l - 1) _s_l[-1] _s_l -1 K // _l - 1 K // _l K _l 1 max(_s_l) max _s_l _ans = max(_s_l[-1] * (K // _l) + max(_s_l[:K % _l]),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) _ans max(_s_l[-1] * (K // _l) + max(_s_l[:K % _l]),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) max _s_l[-1] * (K // _l) + max(_s_l[:K % _l]) _s_l[-1] * (K // _l) _s_l[-1] _s_l -1 K // _l K _l max(_s_l[:K % _l]) max _s_l[:K % _l] _s_l :K % _l K % _l K _l _s_l[-1] * (K // _l - 1) + max(_s_l) _s_l[-1] * (K // _l - 1) _s_l[-1] _s_l -1 K // _l - 1 K // _l K _l 1 max(_s_l) max _s_l ans = max(ans, _ans) ans max(ans, _ans) max ans _ans print(ans) print ans _ans = max(_s_l[-1] * (K // _l) + max(_s_l[:K % _l]),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) max(_s_l[-1] * (K // _l) + max(_s_l[:K % _l]),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) _ans _s_l = [] [] _s_l _cumsum = 0 0 _cumsum _v = s s _v _ans = max(_s_l) max(_s_l) _ans _ans = max(_s_l[-1] * (K // _l),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) max(_s_l[-1] * (K // _l),
                           _s_l[-1] * (K // _l - 1) + max(_s_l)) _ans K = map(int, input().split()) map(int, input().split()) K ans = float("-Inf") float("-Inf") ans _ans = max(_s_l[:K]) max(_s_l[:K]) _ans C_L = [int(x) for x in input().split()] [int(x) for x in input().split()] C_L _v = _nextv _nextv _v N, K = map(int, input().split()) map(int, input().split()) N _l = len(_s_l) len(_s_l) _l ans = max(ans, _ans) max(ans, _ans) ans P_L = [int(x)-1 for x in input().split()] [int(x)-1 for x in input().split()] P_L _cumsum += C_L[_nextv] C_L[_nextv] _cumsum _nextv = P_L[_v] P_L[_v] _nextv _ans = float("-Inf") float("-Inf") _ans
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)
