#include<bits/stdc++.h>
#define ll long long 
#define pb push_back
#define bg begin()
#define en end()
#define vvl(n,m) vector<vector<ll> > a( n , vector<ll> (m))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
/*ll gcd(ll a,ll b){
    if(a==0)
    return b;
    else
    return gcd(b%a,a);
}
ll maxSubArraySum(ll a[], ll size) 
{ 
    ll max_so_far = -1000000000000000, max_ending_here = 0; 
  
    for (ll i = 0; i < size; i++) 
    { 
        max_ending_here = max_ending_here + a[i]; 
        if (max_so_far < max_ending_here) 
            max_so_far = max_ending_here; 
  
        if (max_ending_here < 0) 
            max_ending_here = 0; 
    } 
    return max_so_far; 
} 
ll countPairs(ll arr[], ll n) 
{ 
    map<ll, ll> map; 
    for (ll i = 0; i < n; i++) {
        if (map.find(arr[i]-i) == map.end()) { 
            map[arr[i]-i] = 1; 
        } 
        else { 
            map[arr[i]-i]++; 
        } 
    }
    ll res = 0; 
    for (auto x : map) { 
        ll cnt = x.second; 

        res += ((cnt * (cnt - 1)) / 2); 
    } 
  
    return res; 
} 
ll findSubarraySum(ll arr[], ll n, ll sum) 
{ 
    unordered_map<ll, ll> prevSum; 
  
    int res = 0; 
  
    int currsum = 0; 
  
    for (int i = 0; i < n; i++) { 
        currsum += arr[i]; 
        if (currsum == sum)  
            res++;         
  
        if (prevSum.find(currsum - sum) !=  
                                  prevSum.end())  
            res += (prevSum[currsum - sum]);
        prevSum[currsum]++; 
    } 
  
    return res; 
} 
ll sod(ll x){
    ll r,sum=0;
    while(x>0){
        r=x%10;
        x/=10;
        sum+=r;
    }
    return sum;
}
long long modpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}*/
int main()
{
    fast
    ll n,k,i,s,r,l=0,j,max=LLONG_MIN,t,maxg=LLONG_MIN,max1=LLONG_MIN,max2=LLONG_MIN,y,z,ans,b;
    cin>>n>>k;
    ll p[n],c[n],sum[n+1];
    for(i=0;i<n;i++){
        cin>>p[i];
    }
    for(i=0;i<n;i++){
        cin>>c[i];
    }
    for(i=0;i<n;i++){
        for(j=0;j<n+1;j++){
        sum[j]=0;
        }
        l=0;
        r=i;
        max=LLONG_MIN;
        max1=LLONG_MIN;
        while(1){
            r=p[r]-1;
            if(l==0)
            sum[l]=c[r];
            else
            sum[l]=sum[l-1]+c[r];
            l++;
            if(r==i){
                break;
            }
        }
        for(j=0;j<l;j++){
            if(sum[j]>max){
                max=sum[j];
            }
        }
        if(k/l==0){
            for(j=0;j<k;j++){
                if(sum[j]>max2)
                max2=sum[j];
            }
            ans=max2;
        }
        else if(sum[l-1]<=0){
            ans=max;
        }
        else{
            y=k/l;
            z=k%l;
            for(j=0;j<z;j++){
                if(sum[j]>max1)
                max1=sum[j];
            }
            if(((y-1)*sum[l-1]+max)>=y*sum[l-1]+max1)
            ans=((y-1)*sum[l-1]+max);
            else
            ans=y*sum[l-1]+max1;
        }
        if(ans>maxg)
        maxg=ans;
    }
    cout<<maxg;
}

Measured memory_bytes: 3732