#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, K;
    cin >> N >> K;
    vector<int> p(N);
    vector<ll> C(N);
    vector<vector<pair<int, ll>>> graph(N);
    vector<vector<int>> rev(N);
    for(int i = 0; i < N; ++i){
        cin >> p[i];
        --p[i];
        rev[p[i]].emplace_back(i);
    }
    for(int i = 0; i < N; ++i){
        cin >> C[i];
        graph[i].emplace_back(p[i], C[i]);
    }

    vector<int> vs;
    vector<bool> used(N, false);
    auto dfs = [&](auto&& self, int from) -> void {
        used[from] = true;
        for(auto& e : graph[from]){
            int to = e.first;
            if(!used[to])    self(self, to);
        }
        vs.emplace_back(from);
    };
    for(int v = 0; v < N; ++v){
        if(!used[v])    dfs(dfs, v);
    }
    reverse(vs.begin(), vs.end());
    
    int k = 0;
    vector<int> idx(N, -1);
    auto rdfs = [&](auto&& self, int from) -> void {
        idx[from] = k;
        for(int to : rev[from]){
            if(idx[to] == -1)   self(self, to);
        }
    };
    for(int v = 0; v < N; ++v){
        if(idx[v] == -1){
            rdfs(rdfs, v);
            ++k;
        }
    }

    vector<vector<int>> scc_set(k);
    for(int i = 0; i < N; ++i){
        scc_set[idx[i]].emplace_back(i);
    }

    vector<ll> cycle_sum(k);
    auto calc = [&](auto&& self, int from, int count = 0, ll sum = 0) -> void {
        if(count == (int)scc_set[idx[from]].size()){
            cycle_sum[idx[from]] = sum;
            return;
        }
        int to = p[from];
        self(self, to, count + 1, sum + C[to]);
    };
    for(int i = 0; i < k; ++i){
        calc(calc, scc_set[i][0]);
    }

    ll ans = -1e+10;
    for(int u = 0; u < N; ++u){
        int cur = p[u];
        ll total = C[cur];
        chmax(ans, total);
        int bound = scc_set[idx[u]].size();
        if(bound >= K){
            int count = 1;
            while(count++ < K){
                cur = p[cur];
                total += C[cur];
                chmax(ans, total);
            }
        }
        else{
            int X = K / (int)scc_set[idx[u]].size();
            int num = K % (int)scc_set[idx[u]].size();
            if(!num){
                chmax(ans, cycle_sum[idx[u]] * X);
                chmax(ans, cycle_sum[idx[u]] * (X - 1) + total);
                while(cur != u){
                    cur = p[cur];
                    total += C[cur];
                    chmax(ans, total);
                    chmax(ans, cycle_sum[idx[u]] * (X - 1) + total);
                }
            }
            else{
                chmax(ans, cycle_sum[idx[u]] * X);
                chmax(ans, cycle_sum[idx[u]] * X + total);
                int count = 1;
                while(cur != u){
                    cur = p[cur];
                    total += C[cur];
                    chmax(ans, total);
                    if(++count <= num){
                        chmax(ans, cycle_sum[idx[u]] * X + total);
                    }
                    else{
                        chmax(ans, cycle_sum[idx[u]] * (X - 1) + total);
                    }
                }
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 4640