#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iomanip>
#include <string>
#include <unordered_map>

using namespace std;
typedef long long LL;
template <typename S, typename T> ostream& operator<<(ostream& os, const pair<S,T>& x);
template <typename T> ostream& operator<<(ostream& os, const vector<T>& x);
template <typename T> ostream& operator<<(ostream& os, const set<T>& v);
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& v);
template <typename S, typename T> ostream& operator<<(ostream& os, const unordered_map<S,T>& v);
template <typename S, typename T> ostream& operator<<(ostream& os, const map<S,T>& v);

vector<LL> readvec(LL length, LL off = 0);
#define ALL(x) x.begin(), x.end()
#define REP(x) for(LL repvar = 0; repvar < x; repvar++)

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    LL n, K; cin >> n >> K;
    vector<LL> P = readvec(n,-1);
    vector<LL> C = readvec(n);

    LL ans = -1e15;
    for(LL start = 0; start < n; start++){
        vector<LL> v;
        LL p = start;
        LL cycle_sum = 0;
        while(true){
            p = P[p];
            v.push_back(C[p]);
            cycle_sum += C[p];
            if(p == start) break;
        }
        LL S = 0;
        for(LL end = 0; end < min((LL)v.size(), K); end++){
            S += v[end];
            LL moves_left = K - (end+1);
            LL rounds = cycle_sum > 0 ? moves_left / v.size() : 0;
            ans = max(ans, S + cycle_sum * rounds);
        }
        
    }
    cout << ans << "\n";
}

















/*
Utilities
*/

template <typename S, typename T>
ostream& operator<<(ostream& os, const unordered_map<S,T>& v){
    os << "[";
    for(auto it = v.begin(); it != v.end(); it++) {
        if(it != v.begin()) os << ", ";
        os << it->first << ": " << it->second;
    }
    os << "]";
    return os;
}


template <typename S, typename T>
ostream& operator<<(ostream& os, const map<S,T>& v){
    os << "{";
    for(auto it = v.begin(); it != v.end(); it++) {
        if(it != v.begin()) os << ", ";
        os << it->first << ": " << it->second;
    }
    os << "}";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
    os << "[";
    for(auto it = v.begin(); it != v.end(); it++) {
        if(it != v.begin()) os << ", ";
        os << *it;
    }
    os << "]";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v){
    os << "[";
    for(auto it = v.begin(); it != v.end(); it++) {
        if(it != v.begin()) os << ", ";
        os << *it;
    }
    os << "]";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v){
    os << "[";
    for(auto it = v.begin(); it != v.end(); it++) {
        if(it != v.begin()) os << ", ";
        os << *it;
    }
    os << "]";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S,T>& x){
    os << "(" << x.first << ", " << x.second << ")";
    return os;
}

vector<LL> readvec(LL length, LL off){
    vector<LL> v;
    for(LL i = 0; i < length; i++){
        LL x; cin >> x; v.push_back(x + off);
    }
    return v;
}


Measured memory_bytes: 3716