// Moving_Piece 
// https://atcoder.jp/contests/abc175/tasks/abc175_d
// date: 2020-08-16T00:22:11.064468

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

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=(ll) a;i<(ll) b;i++)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _repd(i,n) repdi(i,0,n)
#define repdi(i,a,b) for(ll i=(ll) b-1;i>=(ll) a;i--)
#define repd(...) _overload3(__VA_ARGS__,repdi,_repd,)(__VA_ARGS__)

#define all(x) x.begin(),x.end()
#define SIZE(x) ((ll)(x).size())

#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev

#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

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; }

template<typename T, typename ...Args>
void CIN(T& first, Args&... args) {
    ((cin >> first), ..., (cin >> args)); //C++17
}

template<char Sep, typename T, typename ...Args>
void print_seq(const T& first, const Args&... args) {
    ((cout << first), ..., (cout << Sep << args)); //C++17
}

template<typename... Words>
void COUT_NL(Words... words) {
    print_seq<' '>(words...);
    return;
}

template<typename... Words>
void COUT(Words... words) {
    print_seq<' '>(words...);
    cout << endl;
    return;
}

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl

#define INF 1000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange

int solve(void) {
    ll N,K;
    CIN(N,K);
    vector<ll> c(N);
    vector<ll> p(N);

    rep(i,N) CIN(p[i]);
    rep(i,N) p[i] -= 1; // to index
    rep(i,N) CIN(c[i]);

    vector<ll> roll_rank(N);
    unordered_set<ll> resolved;
    rep(i,N) {
        ll masu = i;
        if(resolved.count(i)) continue;
        unordered_set<ll> myset;
        rep(j, N + 1) {
            if (myset.count(masu)) {
                for (auto k = myset.begin(); k != myset.end(); k++){
                    resolved.insert(*k);
                    roll_rank[*k] = j;
                }
                break;
            }
            myset.insert(masu);
            masu = p[masu];
        }
    }

    vector<ll> roll_score(N);
    rep(i,N) {
        ll roll = 0;
        ll masu=i;
        rep(j,roll_rank[i]) {
            masu = p[masu];
            roll += c[masu];
        }
        roll_score[i] = roll;
    }

    vector<ll> local_maxs(N); // iスタートで1~k回わましたときのmax
    rep(i,N) {
        ll masu = i;
        ll local_score = 0;
        ll local_max = -1e18;
        ll k = (K - 1) % roll_rank[i] + 1; //k=[1..N] 割り切るときroll_rank
        rep(j, k) {
            local_score += c[masu];
            chmax(local_max, local_score);
            masu = p[masu];
        }
        local_maxs[i] = local_max;
    }
    vector<ll> n_maxs(N); // iスタートで1~N回わましたときのmax
    rep(i,N) {
        ll masu = i;
        ll local_score = 0;
        ll local_max = -1e18;
        rep(j, roll_rank[i]) {
            local_score += c[masu];
            chmax(local_max, local_score);
            masu = p[masu];
        }
        n_maxs[i] = local_max;
    }

//    rep(i,N) {
//        cout << roll_rank[i] << ",";
//    }
//    cout << endl;
//    rep(i,N) {
//        cout << local_maxs[i] << ",";
//
//    }
//    cout << endl;
//    rep(i,N) {
//        cout << roll_score[i] << ",";
//    }
//    cout << endl;

    ll ans_max=-1e18;
    rep(i,N) {
        if (roll_score[i] <= 0) {
            if (K>=roll_rank[i]) chmax(ans_max,n_maxs[i]);
            else chmax(ans_max,local_maxs[i]);
        } else {
            ll loop = (K-1) / roll_rank[i]; //K=lNのとき、loop=l-1
//            COUT(loop,local_maxs[i], (roll_score[i] * loop),local_maxs[i] + (roll_score[i] * loop));
            chmax(ans_max, local_maxs[i] + (roll_score[i] * loop));
        }
    }
    COUT(ans_max);
    return 0;
}

int main(void){
    //入力の高速化用のコード
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    
    solve();
    return 0;
}


Measured memory_bytes: 4020