#include <bits/stdc++.h>
using namespace std;
typedef long long lol;

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(lol i=0;i<lol(n);i++)
#define REPD(i,n) for(lol i=n-1;i>=0;i--)
#define FOR(i,a,b) for(lol i=a;i<=lol(b);i++)
#define FORD(i,a,b) for(lol i=a;i>=lol(b);i--)
#define VL vector<lol>
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) lol(x.size()) //sizeをsize_tからllに直しておく
//定数
#define PI 3.1415926535897932385 //pi
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//最大値最小値
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main()
{
    lol result{0};
    lol n; cin >> n;
    lol k; cin >> k;
    lol MAofn{-10000000000};
    VL p(n+1, 0), c(n+1, 0); //p is pointing next node
    c[0] = -10000000000;
    bool nega = true;
    FOR(i, 1, n)
    {
        cin >> p[i];
    }
    FOR(i, 1, n)
    {
        cin >> c[i];
        if(c[i] > 0) nega = false;
        if(c[i] > MAofn)
            MAofn = c[i];
    }
    VL group(n+1, 0), groupnum(n+1, 0), grouproop(n+1, 0);
    lol count{1};
    FOR(i, 1, n)
    {
        bool fl = true;
        lol cur = i;
        if(group[cur]) continue;
        group[cur] = count;
        groupnum[count]++;
        //grouproop[count] += c[p[cur]];
        while(fl)
        {
            
            if(!group[p[cur]])
            {
                group[p[cur]] = count;
                grouproop[count] += c[p[cur]];
                //cout << count << ":" << c[p[cur]] << grouproop[count] << " ";
                cur = p[cur];
                groupnum[count]++;
                
            }
            else
            {
                lol next = p[cur];
                grouproop[count] += c[p[cur]];
                //cout << count << ":" << c[p[cur]] << grouproop[count] << " ";
                cur = p[cur];
                count++;
                fl = false;
                break;
            }
            
        }
    }
    //cout << endl;
    lol savek = k;
    lol MA{0};
    lol nowmax{-1};
    FOR(i, 1, n)
    {
        k = savek;
        lol save{0};
        lol cur = i;
        lol groupofi = group[i], groupnumofi = groupnum[groupofi], grouproopofi = grouproop[groupofi];
        if(groupnumofi < k)
        {
            //cout << "i:" << i << endl;
            //cout << "grouproopofi" << grouproopofi << endl;
            if(grouproopofi > 0)
            {
                lol kaisuu = k/groupnumofi;
                save += grouproopofi*(kaisuu-1);
                //cout << save << "a ";
                k = k%groupnumofi;
                FOR(j, 1, groupnumofi)
                {
                    save += c[p[cur]];
                    cur = p[cur];
                    nowmax = max(nowmax, save);
                }
            }
            else
            {
                k = groupnumofi;
            }
            nowmax = max(nowmax, save);
            FOR(j, 1, k)
            {
                save += c[p[cur]];
                //cout << save << "b ";
                cur = p[cur];
                nowmax = max(nowmax, save);
                //cout << "nowmax:" << nowmax << endl;
            }
        }
        else
        {
            FOR(j, 1, k)
            {
                save += c[p[cur]];
                //cout << save << "c ";
                cur = p[cur];
                nowmax = max(nowmax, save);
            }
        }
        //cout << endl;
        
        MA = nowmax;
    }

    result = MA;
    if(nega)
    {
        //cout << MAofn << endl;
        result = MAofn;
    }
    cout << result << endl;
    return 0;
}

Measured memory_bytes: 3828