#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;

#define pb                push_back
#define all(x)            (x).begin(),(x).end()
#define int               long long
#define INF               2e18
#define ff                first
#define ss                second
#define vi                vector<int>
#define pii               pair<int,int>
#define mii               map<int,int>
#define endl              "\n"
#define GCD(x,y)          (__gcd((x), (y)))
#define LCM(x,y)          (((x)/__gcd((x), (y)))*(y))
#define debug(x)          cout<<#x<<" is "<<(x)<<endl
#define mem(a,x)          memset(a,x,sizeof(a))
#define rep(i,a,b)        for(long long i=a;i<b;i++)
#define sp(ans,p)         fixed <<setprecision(p)<<ans;
#define IOS ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);

typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

// constexpr int N=;

void solve()
{

    int N, K;
    cin >> N >> K;

    vi P(N);
    vi C(N);
    rep(i, 0, N)
    {
        cin >> P[i];
        P[i]--;
    }
    rep(i, 0, N)
    {
        cin >> C[i];
    }


    int ans = -1 * INF;

    rep(i, 0, N)
    {
        int circnt = 0;
        int cirsum = 0;

        int v = i;

        while (true)
        {
            circnt++;
            cirsum += C[P[v]];
            v = P[v];
            if (v == i) break;
        }
        // debug(cirsum);
        // debug(circnt);

        v = i;
        int compsum = (K / circnt) * cirsum;
        if (compsum < 0) compsum = 0;
        int rem = K % circnt;
        int toaddans = 0;
        if (rem || compsum == 0)
        {
            toaddans = -1 * INF;
            if (compsum == 0)
                rem = min(K, circnt );
            int toadd = 0;
            while (true)
            {
                // circnt++;
                toadd += C[P[v]];
                v = P[v];
                toaddans = max(toaddans, toadd);
                rem--;
                if (rem == 0) break;
                if (v == i) break;
            }
        }
        ans = max(ans, compsum + toaddans);

        /*
        3 7
        2 3 1
        13 15 - 27
        */
        v = i;
        rem = K;
        if (rem >= circnt and cirsum > 0)
        {
            rem -= rem % circnt;
            compsum = ((rem - 1) / circnt) * cirsum;
            toaddans = 0;//here to add must be positive
            rem = circnt;

            if (rem )
            {

                int toadd = 0;
                while (true)
                {
                    // circnt++;
                    toadd += C[P[v]];
                    v = P[v];
                    toaddans = max(toaddans, toadd);
                    rem--;
                    if (rem == 0) break;
                    if (v == i) break;
                }
            }
        }
        ans = max(ans, compsum + toaddans);
    }
    cout << ans << endl;
}



signed main()
{
    IOS;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    // sieve();
    int t = 1;
    // cin >> t;
    while (t--)   solve();
    return 0;
}


Measured memory_bytes: 3652