#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp> 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds;   
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> 
ll mod=1e9+7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a,ll b){if(a>b){return a;}return b;}
ll min(ll a,ll b){if(a<b){return a;}return b;}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
 
// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n,k;
vector<ll> adj[5005];
ll vis[5005];
ll p[5005];
ll c[5005];

void dfs(ll u, vector<ll> &v1){
    for (auto i = adj[u].begin(); i != adj[u].end(); ++i)
    {
        if(!vis[*i]){
            vis[*i]=true;
            v1.push_back(c[p[*i]]);
            dfs(*i, v1);
        }
    }
}

int main()
{ 
    fastio;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    cin>>n>>k;
    for (int i = 1; i < n+1; ++i)
    {
        cin>>p[i];
        adj[i].push_back(p[i]);
    }
    for (int i = 1; i < n+1; ++i)
    {
        cin>>c[i];
    }

    ll ans=-inf;
    for (int i = 1; i < n+1; ++i)
    {
        for (int j = 1; j < n+1; ++j)
        {
            vis[j]=false;
        }
        vis[i]=true;
        vector<ll> v;
        v.push_back(c[p[i]]);
        dfs(i, v);

        // for (int i = 0; i < v.size(); ++i)
        // {
        //     cout<<v[i]<<" ";
        // }
        // cout<<endl;

        ll sum=v[0];
        ans=max(ans, sum);
        if(k<=(v.size()-1)){
            for (int i = 1; i < min(k, v.size()); ++i)
            {
                sum+=v[i];
                ans=max(ans, sum);
            }
        }
        else{
            for (int i = 1; i < min(k, v.size()); ++i)
            {
                sum+=v[i];
                ans=max(ans, sum);
            }
            ll x = k/v.size();
            ll rem = k%v.size();
            if(sum>0){
                if(rem!=0){
                    ll max1=0;
                    ll sum1=0;
                    for (int i = 0; i < rem; ++i)
                    {
                        sum1+=v[i];
                        max1 = max(max1, sum1);
                    }
                    ll tot = sum*x;
                    ans = max(ans, tot + max1);
                }
                else{
                    ll max1=0;
                    ll sum1=0;
                    for (int i = 0; i < v.size(); ++i)
                    {
                        sum1+=v[i];
                        max1 = max(max1, sum1);
                    }
                    ll tot = sum*(x-1);
                    ans = max(ans, tot + max1);
                }
            }
        }
    }
    cout<<ans;

    return 0;
}


Measured memory_bytes: 4452