//#include <bits/stdc++.h>
#include<iostream>
#include <iosfwd>
#include<limits.h>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <limits>
#include<stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include<string.h>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include<vector>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define fr(i,j,n)   for(lli i=j;i<(n);++i)
#define bk(i,j,n)   for(lli i=j;i>=n&&j>=n;--i)
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mem(p,v) memset(p,v,sizeof(p))
#define present(c,x)    ((c).find(x) != (c).end())
#define run ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define debug4(x,y,z,a) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<" "<<#a<<" "<<a<<endl;
#define ub upper_bound
#define lb lower_bound
typedef long long int lli;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<lli> vl;
typedef pair<lli,lli> pl;
inline lli max(lli a,lli b){ return a>b?a:b;}
inline lli min(lli a,lli b){ return a<b?a:b;}
const long long inf = 1e15;
const lli MOD =998244353 ;
void show(vector<lli> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(vector<int> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(int a[], int arraysize){for (int i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(lli a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(bool a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(set<int> s){for (auto it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout<<endl;}
void show(map<lli,lli> mp){for (auto it = mp.begin(); it != mp.end(); it++){cout << it->first << " "<<it->second<<endl;}cout<<endl;}
//Pair hashing is done using std::map ;
lli modpow(lli x,lli n){lli res=1;while(n>0){if(n&1) res=((res%MOD)*(x%MOD))%MOD;x=((x%MOD)*(x%MOD))%MOD;n>>=1;}return res;}
lli power(lli x, lli n){lli res=1;while(n>0){if(n&1) res=res*x;x=x*x;n>>=1;}return res;}

/////// ------ Main code starts here ------ /////
const int N=5005;
int p[N];
int c[N];
int visited[N];
vi gp[N];
lli n,k;
lli sum=0;
lli ans;
vl dp(N);
vl mx(N);
void dfs(lli s,lli t)
{
    if(t>=k)
        return;
    if(visited[s])
    {
        if(sum>=0)
        {
            if(k%t)
            {
                ans=max(ans,sum*(k/t)+mx[k%t-1]);
            }
            else
            {
                ans=max(ans,sum*(k/t));
                ans=max(ans,sum*(k/t-1)+mx[t-1]);
            }
        }
        else
        {
            ans=max(ans,sum+mx[k%t]);
        }
        return;
    }
    visited[s]=1;
    for(int i:gp[s])
    {
        sum+=c[i];
        dp[t]=sum;
        if(t==0)
        {
            mx[t]=sum;
        }
        else
        {
            mx[t]=max(sum,mx[t-1]);
        }
        ans=max(ans,sum);
        dfs(i,t+1);
    }
}
void solve()
{
    cin>>n>>k;
    fr(i,0,n)
    {
        cin>>p[i];
        p[i]-=1;
        gp[i].pb(p[i]);
    }
    fr(i,0,n)
    {
        cin>>c[i];
    }
    mem(visited,0);
    ans=-inf;
    for(int i=0;i<n;++i)
    {
        fr(j,0,n)
        {
            visited[j]=0;
        }
        sum=0;
        dfs(i,0);
    }
    //if(ans)
    cout<<ans<<endl;
}
int main()
{
    run;cout<<fixed<<setprecision(12);
    int t;
    t=1;
    while(t){solve();t--;}
    return 0;
}


Measured memory_bytes: 4444