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

vector<vector<long long>>cycles;
void solve(){
    int n;
    cin>>n;
    long long k;
    cin>>k;
    vector<long long>p(n);
    for(int i=0; i<n;i++){
        cin>>p[i];
        p[i]--;
    }
    vector<long long>c(n);
    for(int i=0; i<n;i++){
        cin>>c[i];
    }
    vector<bool>vis(n, false);
    for(int i=0; i<n;i++){
        if(vis[i])continue;
        int curr = i;
        cycles.emplace_back();
        while(vis[curr] == false){
            cycles.back().push_back(c[curr]);
            vis[curr] = true;
            curr = p[curr];
        }
    }
    long long ans = *max_element(c.begin(), c.end());
    for(int i=0; i<cycles.size();i++){
        long long curr = 0;
        for(int j=0; j<cycles[i].size(); j++){
            curr += cycles[i][j];
        }
        
        if(curr < 0){
            curr = 0;
            if(k%cycles[i].size() == 0){
                for(int j=0;j<cycles[i].size(); j++){
                    long long sum = 0;
                    int right = j;
                    for(int kk = 0;kk<cycles[i].size(); kk++){
                        sum += cycles[i][right];
                        right++;
                        right%=cycles[i].size();
                        ans = max(ans,sum+curr);
                    }
                }
            }
            else{
                if(k > cycles.size()){
                    for(int j=0;j<cycles[i].size(); j++){
                    long long sum = 0;
                    int right = j;
                    for(int kk = 0;kk<cycles[i].size(); kk++){
                        sum += cycles[i][right];
                        right++;
                        right%=cycles[i].size();
                        ans = max(ans,sum+curr);
                    }
                }
                }
                else{for(int j=0;j<cycles[i].size(); j++){
                    long long sum = 0;
                    int right = j;
                    for(int kk = 0;kk<k%cycles[i].size(); kk++){
                        sum += cycles[i][right];
                        right++;
                        right%=cycles[i].size();
                        ans = max(ans,sum+curr);
                    }
                }}
            }
        }
        else if(cycles[i].size() > k){
            curr = 0;
            for(int j=0;j<cycles[i].size(); j++){
                long long sum = 0;
                int right = j;
                for(int kk = 0;kk<k; kk++){
                    sum += cycles[i][right];
                    right++;
                    right%=cycles[i].size();
                    ans = max(ans,sum+curr);
                }
            }
        }
        else{
            if(k%cycles[i].size() == 0){
                curr *= (k/cycles[i].size()-1);
                for(int j=0;j<cycles[i].size(); j++){
                    long long sum = 0;
                    int right = j;
                    for(int kk = 0;kk<cycles[i].size(); kk++){
                        sum += cycles[i][right];
                        right++;
                        right%=cycles[i].size();
                        ans = max(ans,sum+curr);
                    }
                }
            }
            else{
                curr *= k/cycles[i].size();
                for(int j=0;j<cycles[i].size(); j++){
                    long long sum = 0;
                    int right = j;
                    for(int kk = 0;kk<k%cycles[i].size(); kk++){
                        sum += cycles[i][right];
                        right++;
                        right%=cycles[i].size();
                        ans = max(ans,sum+curr);
                    }
                }
            }
        }
    }
    cout<<ans;
}

int main() {
    int t;
    t = 1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


Measured memory_bytes: 3808