/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include <iostream>
#include "bits/stdc++.h"

using namespace std;
typedef unsigned long long ll;
typedef int                     integer;
#define int                     long long
#define ii                      pair<int,int>
#define vii                     vector<pair<int,int>>
#define vi                      vector<int>
#define vvi                     vector<vector<int>>
#define f(a,b,c)                for(int a=b;a<c;a++)
#define s(x)                    in>>x
#define s2(x,y)                 in>>x>>y
#define s3(x,y,z)               in>>x>>y>>z
#define s4(x,y,z,w)             in>>x>>y>>z>>w
#define p(x)                    out<<x<<endl
#define p2(x,y)                 out<<x<<' '<<y<<endl
#define p3(x,y,z)               out<<x<<' '<<y<<' '<<z<<endl
#define p4(x,y,z,w)             out<<x<<' '<<y<<' '<<z<<' '<<w<<endl
#define pl(x)                   out<<x
#define pll(x)                  out<<x<<' '
#define range(v)                v.begin(),v.end()
#define rst(x)                  memset(x, -1, sizeof(x))
#define UNVISITED               -1
#define VISITED                 0
#define EXPLORED                2

#define EACH(x, a) for (auto& x: a)
template<class T> void read_v(T& x, std::istream& in) {
    in >> (x);
}
template<class A> void read_vec(vector<A>& x, std::istream& in) {
    EACH(a, x)
        read_v(a, in);
}

bool sortpairbysec(const pair<int,int> &a,const pair<int,int> &b){return (a.second < b.second);}
template <typename T>
T factmod(T a, T modulus){ int res = 1, i; for (i = 2; i <= a; i++){ res *= i; res%=modulus;} return res; }
template <typename T>
int powmod(T a,T b,int m){ int ans=1; while(b>0){if(b&1){ans*=a;ans%=m;}a*=a;a%=m;b>>=1;} return ans; }
template <typename T>
int power(T a, T b){ int ans=1; while(b>0){if(b&1){ans*=a;}a*=a;b>>=1;}return ans;}


void ssolve(std::istream& in, std::ostream& out){
    int n, k; s2(n, k);
    vector<int> pe(n + 1);
    f(i, 1, n + 1){
        s(pe[i]);
    }
    vector<int> c(n + 1);
    f(i, 1, n + 1){
        s(c[i]);
    }
    int ans = LONG_LONG_MIN;
    int curr = 0;
    int cycle_len = 0;
    vector<int> cs_cycle(n + 1, 0);
    for(int i = 1; i <= n; i++){
        int start = i;
        curr = i;
        cycle_len = 1;
        cs_cycle[cycle_len] = c[pe[i]];
        while(pe[curr]!=start){
           cycle_len++;
           curr = pe[curr];
           cs_cycle[cycle_len] = cs_cycle[cycle_len - 1] + c[pe[curr]];
        }

        int max_node = LONG_LONG_MIN;
        int complete_walks = k/cycle_len;
        int non_complete_walks = k - cycle_len * complete_walks;
        int complete_cycle_value = cs_cycle[cycle_len];
        int curr_base = 0;
        int add = 0;
        if (complete_cycle_value > 0){
            curr_base = (complete_walks * complete_cycle_value);
            max_node = max(max_node, curr_base);
            f(ix, 0, non_complete_walks){
                add = cs_cycle[ix + 1];
                max_node = max(max_node, curr_base + add);
            }
            add = 0;
            if (complete_walks>=1){
                curr_base = ((complete_walks - 1) * complete_cycle_value);
                max_node = max(max_node, curr_base);
                f(ix, 0, cycle_len){
                    add = cs_cycle[ix + 1];
                    max_node = max(max_node, curr_base + add);
                }
            }
        }else{
            if (complete_walks>=1){
                f(ix, 0, cycle_len){
                    add = cs_cycle[ix + 1];
                    max_node = max(max_node, add);
                }
            }else{
                f(ix, 0, non_complete_walks){
                    add = cs_cycle[ix + 1];
                    max_node = max(max_node, add);
                }
            }
        }
        ans = max(ans, max_node);
    }
    p(ans);
}

class DMovingPiece {
public:
void solve(std::istream& in, std::ostream& out) {
    ios_base::sync_with_stdio(false);
    in.tie(nullptr);out.tie(nullptr);
    int t;
    int READ = 0;
    if(READ) s(t);
    else t = 1;
    while(t--){
        ssolve(in, out);
    }
}
};

integer main() {
    DMovingPiece solver;
    std::istream& in(std::cin);
     std::ostream& out(std::cout);
     solver.solve(in, out);
}

Measured memory_bytes: 3736