#include <bits/stdc++.h>
//using
using intl = long long;
using itnl = long long;//typo用
using uintl = unsigned long long;
using itn = int;//typo用
using ld = long double;
using namespace std;

//関数マクロ
#define rep(i, n) for(intl i = 0; i < (intl)(n); i++)
#define rrep(i, n) for(intl i = (intl)(n) - 1; i >= 0; i--)
#define repi(i, a, b) for(intl i = (intl)(a); i < (intl)(b); i++)
#define rrepi(i, a, b) for(intl i = (intl)(a) - 1; i >= (intl)(b); i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,1,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define alength(a) (sizeof(a) / sizeof(a[0]))
#define debug(x) cout << #x << ":" << x << endl

//定数マクロ
#define pb push_back
#define mp make_pair
#define pii pair<intl,intl>

//定数
const intl INF = 1e9;
const intl MOD = 1e9+7;
const ld EPS = 1.0e-14;//-9
const ld PI = acos(-1);

//テンプレート関数
template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} return false; }
//intl gcd(intl a, intl b){ if(!b)return a; return gcd(b, a % b); } //c++17からある
//intl lcm(intl a,intl b){ return a / gcd(a, b) * b; } //c++17からある

//自作テンプレート関数
void fp(bool f){cout << (f ? "Yes" : "No") << endl;}//Yes,Noの出力を楽にするよう
void fp(bool f, string s, string t){cout << (f ? s : t) << endl;}//上の関数のYes,Noを任意の文字列でできるようにしたもの
intl fact(intl k){ intl a = 1; for(int i = 2; i <= k; i++){ a *= i; } return a; }//k!を求める関数,O(N)
intl digit10(intl a){ intl b = 0; do{ a /= 10; b++; }while(a); return b; }//aを10進数で表したときの桁数を求める関数,O(logN)




signed main(){
    cout << fixed << setprecision(10);
    intl n, k;
    cin >> n >> k;
    intl p[n], c[n];
    rep(i,n)cin >> p[i];
    rep(i,n)cin >> c[i];
    pair<intl, intl> cnt[n];
    m0(cnt);
    intl ans = -INF;
    rep(i,n){
        bool flag[n];
        m0(flag);
        intl idx = i;
        while(true){
            idx = p[idx] - 1;
            if(flag[idx])break;
            cnt[i].first += c[idx];
            ans = max(ans, cnt[i].first);
            cnt[i].second++;
            if(cnt[i].second >= k)break;
            flag[idx] = true;
        }
    }
    //for(auto i : cnt)cout << i.first << ":" << i.second << endl;
    //debug(ans);
    rep(i,n){
        intl m = k % cnt[i].second;
        m += cnt[i].second;
        intl b[m];
        m0(b);
        intl num = k / cnt[i].second;
        num--;
        if(num < 0)break;
        b[0] = cnt[i].first * num;
        //if(b[0] < 0)break;
        //debug(b[0]);
        intl idx = i;
        repi(j, 1, m){
            idx = p[idx] - 1;
            b[j] = b[j - 1] + c[idx];
            //debug(b[j]);
        }
        //debug(m);
        repi(j, 1, m){
            ans = max(ans,b[j]);
            //cout << j << ":" << b[j] << endl;
        }
    }
    rep(i,n){
        intl m = k % cnt[i].second;
        m++;
        intl b[m];
        m0(b);
        intl num = k / cnt[i].second;
        //if(!num)break;
        b[0] = cnt[i].first * num;
        //if(b[0] < 0)break;
        //debug(b[0]);
        intl idx = i;
        repi(j, 1, m){
            idx = p[idx] - 1;
            b[j] = b[j - 1] + c[idx];
            //debug(b[j]);
        }
        //debug(m);
        rep(j,m){
            ans = max(ans,b[j]);
            //cout << j << ":" << b[j] << endl;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3832