// dfmt off
T lread(T=long)(){return readln.chomp.to!T;}T[] lreads(T=long)(long n){return iota(n).map!((_)=>lread!T).array;}
T[] aryread(T=long)(){return readln.split.to!(T[]);}void arywrite(T)(T a){a.map!text.join(' ').writeln;}
void scan(L...)(ref L A){auto l=readln.split;foreach(i,T;L){A[i]=l[i].to!T;}}alias sread=()=>readln.chomp();
void dprint(L...)(lazy L A){debug{auto l=new string[](L.length);static foreach(i,a;A)l[i]=a.text;arywrite(l);}}
alias PQueue(T,alias l="b<a")=BinaryHeap!(Array!T,l);import std, core.bitop;
// dfmt on
void main()
{
    auto input = stdin.byLine.map!split.joiner;

    long N;
    N = input.front.to!long;
    input.popFront;

    long K;
    K = input.front.to!long;
    input.popFront;

    long[] P = new long[](cast(size_t)(N));
    foreach (i; 0 .. cast(size_t)(N))
    {
        P[i] = input.front.to!long;
        input.popFront;
    }

    long[] C = new long[](cast(size_t)(N));
    foreach (i; 0 .. cast(size_t)(N))
    {
        C[i] = input.front.to!long;
        input.popFront;
    }

    solve(N, K, P, C);
}

void solve(long N, long K, long[] P, long[] C)
{
    long ans = long.min;
    foreach (start; 0 .. N)
    {
        long len = 1;
        long ptr = P[start] - 1;
        dprint();
        while (ptr != start)
        {
            len++;
            ptr = P[ptr] - 1;
            dprint(ptr);
        }
        ptr = start;
        auto s = new long[](len + 1);
        foreach (i; 0 .. len)
        {
            s[i + 1] = s[i] + C[ptr];
            ptr = P[ptr] - 1;
        }
        dprint(s);
        if (s[$ - 1] < 0)
        {
            ans = ans.max(s[1 .. min(K + 1, $)].reduce!max());
        }
        else
        {
            foreach (i; 0 .. len)
            {
                ans = ans.max(s[i] + s[$ - 1] * ((K - i) / len));
            }
        }
    }
    writeln(ans);
}
