// while( !(succeed == try()) );

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace __gnu_pbds; 
using namespace std;

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha


#define int long long int
#define loop(i,s,e) for(int i=s; i<e; i++)
#define endl '\n'
#define vii vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define N 100001
#define pb push_back
#define INF LLONG_MAX
#define inf INT_MAX

void INP();

#define bug(...)        __f (#__VA_ARGS__, __VA_ARGS__)
#define rtn                                       if(0)return;
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { rtn cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{     rtn 
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; 


// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha


void alpha()
{    
    int n, k;
    cin>>n>>k;

    int p[n], c[n];
    loop(i,0,n) 
    {
        cin>>p[i];
        p[i]--;
    }
    loop(i,0,n) cin>>c[i];

    int dp[n];
    int moves[n];
    int max_found[n];
    int max_moves[n];

    memset(dp, -1, sizeof(dp));

    loop(i,0,n)
    {
        int curr = p[i];
        int sum=0;
        int m = 1;

        int mx = -inf;

        while(curr!=i)
        {
            sum += c[curr];

            if(sum > mx)
            {
                max_moves[i] = m;
                mx = sum;
            }

            curr = p[curr];
            m++;
        }
        sum += c[i];

        dp[i] = sum;
        moves[i] = m;
        max_found[i] = mx;
    }

    int ray[n] = {0};

    loop(i,0,n)
    {
        if(dp[i]<=0)
        {
            if(k >= max_moves[i]) ray[i] = max_found[i];
            else
            {
                int t = k;
                int sum=0;
                int mx = 0;

                int curr = i;
                while(t!=0)
                {
                    t--;
                    curr = p[curr];

                    sum += c[curr];
                    mx = max(mx, sum);
                } 

                ray[i] = mx;
            }
        }
        else
        {
            int t = k;

            int temp1 = (t/moves[i])*dp[i];
            t %= moves[i];

            int curr = i;
            int sum = 0;
            int mx = 0;

            int temp2 = temp1-dp[i]+max_found[i];


            while(t!=0)
            {
                t--;
                curr = p[curr];

                sum += c[curr];
                mx = max(mx, sum);
            } 

            ray[i] = max(temp1+mx, temp2);
        }
    }

    int mx = -inf;
    loop(i,0,n) mx = max(mx, ray[i]);

    cout<<mx<<endl;
}

int32_t main(){

    ios_base:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    // INP();
    int t=1;
    //cin>>t;
    
    while(t--) alpha();

    return 0;

}






















inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}
 
 

Measured memory_bytes: 3972