#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> p(n+1), c(n+1);
    for(int i = 1; i <= n; i++) cin >> p[i];
    for(int i = 1; i <= n; i++) cin >> c[i];

    vector<vector<int>> route;
    vector<bool> isvisit(n, false);
    for(int i = 1; i <= n; i++){
        if(!isvisit[i]){
            route.push_back(vector<int>(1, i));
            int cur = i;
            isvisit[i] = true;
            while(p[cur] != i){
                route[route.size()-1].push_back(p[cur]);
                isvisit[cur] = true;
                cur = p[cur];
            }
        }
    }

    long long int ans = -1001001001001001001;
    for(int i = 0; i < route.size(); i++){
        long long int coans = 0;
        long long int shuuscore = 0;
        if(route[i].size() <= k){
            int shuu = k / (int)route[i].size();
            for(int j = 0; j < route[i].size(); j++){
                shuuscore += c[route[i][j]];
            }
            if(shuuscore > 0){
                coans += shuuscore * shuu;
            }
        }
        
        long long int comax = -1001001001001001001;
        int amarilim;
        if(coans == 0) amarilim = min(k, (int)route[i].size());
        else amarilim = k%route[i].size();
        for(int j = 1; j <= amarilim; j++){
            long long int cosum = 0;
            for(int l = 0; l < j; l++) cosum += c[route[i][l]];
            for(int l = 0; l < route[i].size(); l++){
                if(comax < cosum) comax = cosum;
                cosum -= c[route[i][l]];
                cosum += c[route[i][(l+j)%route[i].size()]];
            }
        }

        if(coans == 0){
            for(int j = 1; j <= min(k, (int)route[i].size()); j++){
                long long int cosum = 0;
                for(int l = 0; l < j; l++) cosum += c[route[i][l]];
                for(int l = 0; l < route[i].size(); l++){
                    if(comax < cosum) comax = cosum;
                    cosum -= c[route[i][l]];
                    cosum += c[route[i][(l+j)%route[i].size()]];
                }
            }
            coans += comax;
            if(ans < coans) ans = coans;
        }else{
            for(int j = 1; j <= min(k, (int)route[i].size()); j++){
                long long int cosum = 0;
                for(int l = 0; l < j; l++) cosum += c[route[i][l]];
                for(int l = 0; l < route[i].size(); l++){
                    if(comax < cosum) comax = cosum;
                    cosum -= c[route[i][l]];
                    cosum += c[route[i][(l+j)%route[i].size()]];
                }
            }
            long long int coans1 = coans + comax - shuuscore;
            comax = -1001001001001001001;
            for(int j = 1; j <= k%route[i].size(); j++){
                long long int cosum = 0;
                for(int l = 0; l < j; l++) cosum += c[route[i][l]];
                for(int l = 0; l < route[i].size(); l++){
                    if(comax < cosum) comax = cosum;
                    cosum -= c[route[i][l]];
                    cosum += c[route[i][(l+j)%route[i].size()]];
                }
            }
            long long int coans2 = coans + comax;
            if(coans1 > ans) ans = coans1;
            if(coans2 > ans) ans = coans2;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3776