#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<functional>
#include<queue>
#include<set>
#include<stack>
#include<iomanip>
#include<algorithm>
#include<unordered_map>
#include <numeric>
#include<string_view>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(i) i.begin(),i.end()
template<typename T =ll> using v = vector<T>;
template<typename T =ll> using vv = v<v<T>>;
template<typename T =ll,typename U =ll> using p = pair<T,U>;
template<typename T> T chmin(T &a,T b) {return a=min(a,b);}
template<typename T> T chmax(T &a,T b) {return a=max(a,b);}
const ll INF = (ll)1e9;
template <int MOD>
struct ModInt {
    using lint = long long;
    int val;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }
    ModInt inv() const { return this->pow(MOD - 2); }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }
    ModInt pow(lint n) const {
        auto x = ModInt(1);
        auto b = *this;
        while (n > 0) {
            if (n & 1) x *= b;
            n >>= 1;
            b *= b;
        }
        return x;
    }

    // compound assignment
    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }

    // compare
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a(){
    string s; cin >> s;
    ll ans = 0;
    ll cnt = s[0]=='R';
    if(s=="RRR") ans = 3;
    else if(s=="RRS") ans = 2;
    else if(s=="SRR") ans = 2;
    else if(s.find_first_of('R')!=string::npos){
        ans = 1;
    }else{
        ans = 0;
    }

    cout << ans << endl;
}

void b(){
    ll N;
    cin >> N;
    auto L = v<>(N,0);
    rep(i,N) cin >> L[i];
    ll cnt = 0;
    rep(i,N) for(ll j=i+1;j<N;j++) for(ll k=j+1;k<N;k++){
        if(i<j&&j<k&&L[i]!=L[j]&&L[j]!=L[k]&&L[i]!=L[k]){
            if(L[i]+L[j]>L[k]&&L[j]+L[k]>L[i]&&L[k]+L[i]>L[j]){
                cnt++;
            }
        }
    }
    cout << cnt << endl;
    
}

void c(){
    ll X,K,D; cin >> X >> K >> D;
     X = abs(X);

    if(X/D-K>0){
        cout << (X/D-K)*D+(X%D!=0?1:0) << endl;
    }else{
        if((K-X/D)%2){
            cout << abs(X%D-D) << endl;
        }else{
            cout << X%D << endl;
        }
    }
}

void d(){
    ll N,K; cin >> N >> K;
    auto P = v<>(N,0); rep(i,N) {
        cin >> P[i];
        P[i]--;
    }

    auto C = v<>(N,0); rep(i,N) cin >> C[i];
    ll ans = -INF;
    rep(i,N){
        ll tot = 0;
        ll j = i;
        auto S = v<>();

        while(1){
            j=P[j];
            tot+=C[j];
            S.push_back(C[j]);
            if(j==i) break;
        }
        ll now = 0;
        ll l = S.size();
        rep(k,l){
            ll cnt = now+=S[k];
            if(K<k+1) break;
            if(tot>0){
                cnt+=tot*((K-k-1)/l);
            }
            chmax(ans,cnt);
        }

    }
    cout << ans << endl;
}

void e(){
   ll R,C,K; cin >> R >> C >> K;
   auto G = v<v<>>(R+1,v<>(C+1,0));
   rep(i,K){
       ll r,c,k;
       cin >> r >> c >> k;
       G[r][c]=k;
   }
   auto dp=v<v<v<>>>(R+1,v<v<>>(C+1,v<>(4,0)));

   rep(i,R+1) rep(j,C+1) rep(k,4){
       //r
       if(i>0)chmax(dp[i][j][0],dp[i-1][j][k]);
       if(i>0)chmax(dp[i][j][1],dp[i-1][j][k]+G[i][j]);
       //c
       if(j>0){
           chmax(dp[i][j][k],dp[i][j-1][k]);
            if(k>0){
                chmax(dp[i][j][k],dp[i][j-1][k-1]+G[i][j]);
            }
       }
       
   }

   cout << *max_element(all(dp[R][C]));
} 

int main(void){
    d();
    return 0;
 }

Measured memory_bytes: 3800