#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<deque>
#include<set>
#include<map>
#include<bitset>
#include<ctime>
#include<queue>
using namespace std;
#define int long long
#define pii pair <int, int>
#define XX first
#define YY second

// constants:
const int mn = 200005;
const int mod = 1000000007;
const long long inf = 4444444444444444444;
const int sminf = 1111111111;
const bool is_multitest = 0;
const bool is_interactive = 0;

// i/o methods:
const bool input_from_file = 0;
const bool output_to_file = 0;
#define filename ""
#define in_extension "inp"
#define out_extension "out"

// data preprocessing: (e.g.: divisor generating, prime sieve)
void preprocess()
{
    
}

// global variables:
int n, k;
int P[mn], C[mn];
vector <vector <int> > cycles;
int passed[mn];

int find_ans(vector <int> v) {
    int sz = v.size();
    vector <int> mcd = {0};
    for (int i:v) mcd.push_back(mcd.back() + C[i]);
    for (int i:v) mcd.push_back(mcd.back() + C[i]);
    for (int i:v) mcd.push_back(mcd.back() + C[i]);
    int sumv = mcd[sz];
    // cerr<<sumv;

    if (sumv > 0) {
        int ret = sumv * (k/sz);
        int rem = k%sz;
        if (k>=sz) {
            ret -= sumv;
            rem += sz;
        }
        // cerr<<ret<<" "<<rem<<"\n";
        int ret2 = 0;
        for (int i=sz+1; i<mcd.size(); i++) {
            for (int j=1; j<=rem; j++) if (i-j>0) ret2 = max(mcd[i] - mcd[i-j], ret2);
        }
        return ret + ret2;
    } else {
        int ret = -inf;
        for (int i=sz+1; i<mcd.size(); i++) {
            for (int j = i-1; j>=max(0ll, i-k); j--) ret=max(ret, mcd[i]-mcd[j]);
        }
        return ret;
    }

    
}
// main solution goes here:
void execute(int test_number)
{
    cin>>n>>k;
    for (int i=1; i<=n; i++) cin>>P[i];
    for (int i=1; i<=n; i++) cin>>C[i];
    cycles = {};
    for (int i=1; i<=n; i++) if (!passed[i]) {
        vector <int> nw = {i};
        passed[i] = 1;
        int u = P[i];
        while (u!=i) {
            passed[u] = 1;
            nw.push_back(u);
            u=P[u];
        }
        cycles.push_back(nw);
    }

    int ans = -inf;
    for (vector <int> v:cycles) ans=max(ans, find_ans(v));
    cout<<ans;
}
// REMEMBER TO CHOOSE TEST METHODS
// PLEASE REMOVE cout AND cerr DEBUG LINES BEFORE SUBMITTING PROBLEMS
// Solution by low_



















signed main()
{
#ifdef lowie
    if (!is_interactive)
    {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
#else
    if (input_from_file) freopen(filename"."in_extension, "r", stdin);
    if (output_to_file) freopen(filename"."out_extension, "w", stdout);
#endif
    if (!is_interactive)
    {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }

    preprocess();
    int ntest;
    if (is_multitest) cin >> ntest;
    else ntest = 1;

    for (int testno = 1; testno <= ntest; testno++)
    // use for instead of while to serve facebook hacker cup test format
    {
        execute(testno); // only start coding in this function, not in main
    }
}
// Template by low_
// Contact me via mail: ttuandung1803@gmail.com
// ...or codeforces: www.codeforces.com/profiles/low_
// ...or if you're interested in food: www.instagram.com/lowie.exe/

Measured memory_bytes: 4000