#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef tree<int,null_type,less<int >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p);
template <class T>
ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v);
#ifdef APURBA
#define HERE cout<<"here - "<<__LINE__<<"\n";
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it)
{
    cout<<endl;
}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
    cout << *it << " = " << a << " ";
    err(++it, args...);
}
#else
#define HERE
#define debug(args...)
#endif
const ll mod = 998244353 ;
const int N = 1e5+5;
typedef pair<int,int> pii;

ll n,k;
vector<vector<ll> >adj;
vector<ll>p;
vector<ll>c;
vector<bool>vis;
vector<vector<ll> >cycles;
void dfs(ll s,vector<ll>&v)
{
    v.push_back(s);
    vis[s]= true;
    for(ll i:adj[s])
    {
        if(!vis[i])
        {
            dfs(i,v);
        }
    }
}

ll fun(vector<ll>&v)
{
    //debug("start")
    ll n = v.size();
    vector<ll>prefix(n);
    for(ll i=0; i<n; i++)
    {
        prefix[i]= c[v[i]];
        if(i>0)
            prefix[i]+= prefix[i-1];
    }
    ll sum = prefix.back();
    //debug(prefix);
    ll ans =  -1e18;
    for(ll i=0; i<n; i++)
    {
        ll now = prefix[i]- prefix[0];
        ll d = i;
        ll koto = k/n;
        ll md = k%n;
        if(d>md)
        {
            koto--;
        }
        if(koto==0 && d==0)
            continue;
        if(koto>=0)
        {
            if(koto==0 && i==0)
            {

            }
            else
            {
                ans = max(ans, sum*koto+now);
            }
            if(i!=0)
                ans = max(ans, now);
        }
    }
    //debug(v,ans);
    return ans;

}

void TEST_CASES(int cas)
{
    scanf("%lld %lld",&n,&k);
    adj.resize(n);
    p.resize(n);
    c.resize(n);
    vis.resize(n);
    for(ll i=0; i<n; i++)
    {
        scanf("%lld",&p[i]);
        p[i]--;
        adj[i].push_back(p[i]);
    }
    for(ll i=0; i<n; i++)
    {
        scanf("%lld",&c[i]);
    }
    debug(p,c);
    for(ll i=0; i<n; i++)
    {
        fill(vis.begin(),vis.end(),false);
        vector<ll>v;
        dfs(i,v);
        cycles.push_back(v);
    }
    ll ans = -1e18;
    for(auto &it:cycles)
    {
        ans = max(ans, fun(it));
    }
    printf("%lld\n",ans);
}


/*

*/

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t=1,cas=0;
    //scanf("%d",&t);
    while(t--)
    {
        TEST_CASES(++cas);
    }
    return 0;
}



template<class T1, class T2>
ostream &operator <<(ostream &os, pair<T1,T2>&p)
{
    os<<"{"<<p.first<<", "<<p.second<<"} ";
    return os;
}
template <class T>
ostream &operator <<(ostream &os, vector<T>&v)
{
    os<<"[ ";
    for(int i=0; i<v.size(); i++)
    {
        os<<v[i]<<" " ;
    }
    os<<" ]";
    return os;
}

template <class T>
ostream &operator <<(ostream &os, set<T>&v)
{
    os<<"[ ";
    for(T i:v)
    {
        os<<i<<" ";
    }
    os<<" ]";
    return os;
}
template <class T1, class T2>
ostream &operator <<(ostream &os, map<T1,T2>&v)
{
    for(auto i:v)
    {
        os<<"Key : "<<i.first<<" , Value : "<<i.second<<endl;
    }
    return os;
}


Measured memory_bytes: 199728