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

#ifdef DEBUG
    #include "debug.h"
    #define dump(...)                                                              \
        DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
                << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
                << endl                                                            \
                << "    ",                                                         \
            dump_func(__VA_ARGS__)
#else
    #define dump(...)
#endif


// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename V>
typename enable_if<is_class<T>::value==0>::type
fill_v(T &t,const V &v){t=v;}

template<typename T,typename V>
typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t,const V &v){
  for(auto &e:t) fill_v(e,v);
}

//////////////////

#define int long long
 
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

#define YES puts("YES")
#define Yes puts("Yes")
#define NO  puts("NO")
#define No  puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
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 (a>b) { a=b; return 1; } return 0; }

/*
#define mod 1000000007 /*/
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL<<60)


void solve(){
    int N, K;
    cin >> N >> K;
    vector<int> P(N);
    vector<int> C(N);
    rep(i,N){
        int p; cin >> p; p--;
        P[i] = p;
    }
    rep(i,N) cin >> C[i];
    
    set<int> s;
    rep(i,N) s.insert(i);
    
    int ans = -INF;
    while(!s.empty()){
        int now = *s.begin();
        vector<int> arr;
        while(s.find(now)!=s.end()){
            arr.push_back(C[now]);
            s.erase(now);
            now = P[now];
        }
        dump(arr);
        int kosu = arr.size();
        
        // 2倍に
        rep(i,kosu) arr.push_back(arr[i]);
        
        vector<int> sum(kosu*2+1,0);
        rep(i,kosu*2) sum[i+1] = sum[i] + arr[i];

        int wa = -INF;
        if(K<=kosu){
            for(int i=0; i<kosu; i++){
                for(int l=1; l<=K; l++){
                    wa = max(wa, sum[i+l] - sum[i]);
                }
            }
        }else{
            int cycle = K / kosu;
            int amari = K % kosu;
            
            if(sum[kosu]>0){
                for(int i=0; i<kosu; i++){
                    for(int l=1; l<=amari; l++){
                        wa = max(wa, sum[i+l] - sum[i]);
                    }
                }
                if(amari==0) wa = 0;
                wa+= sum[kosu] * cycle;
            }else{
                for(int i=0; i<kosu; i++){
                    for(int l=1; l<=amari; l++){
                        wa = max(wa, sum[i+l] - sum[i]);
                    }
                }
                if(amari==0) wa = max(0LL, *max_element(ALL(arr)));
            }
            
            cycle--;
            amari = kosu;
            int wa2 = -INF;
            if(sum[kosu]>0){
                for(int i=0; i<kosu; i++){
                    for(int l=1; l<=amari; l++){
                        wa2 = max(wa2, sum[i+l] - sum[i]);
                    }
                }
                if(amari==0) wa2 = 0;
                wa2+= sum[kosu] * cycle;
            }else{
                for(int i=0; i<kosu; i++){
                    for(int l=1; l<=amari; l++){
                        wa2 = max(wa2, sum[i+l] - sum[i]);
                    }
                }
                if(amari==0) wa2 = max(0LL, *max_element(ALL(arr)));
            }
            chmax(wa, wa2);
        }
        chmax(ans, wa);
    }
    cout << ans << endl;
}


signed main(){
    cout << fixed << setprecision(18);
    cerr << fixed << setprecision(18);
    solve();
}


Measured memory_bytes: 4064