Problem:
Score : 400 points

Problem Statement

Takahashi will play a game using a piece on an array of squares numbered 1, 2, \cdots, N. Square i has an integer C_i written on it. Also, he is given a permutation of 1, 2, \cdots, N: P_1, P_2, \cdots, P_N.

Now, he will choose one square and place the piece on that square. Then, he will make the following move some number of times between 1 and K (inclusive):

In one move, if the piece is now on Square i (1 \leq i \leq N), move it to Square P_i. Here, his score increases by C_{P_i}.

Help him by finding the maximum possible score at the end of the game. (The score is 0 at the beginning of the game.)

Constraints

2 \leq N \leq 5000

1 \leq K \leq 10^9

1 \leq P_i \leq N

P_i \neq i

P_1, P_2, \cdots, P_N are all different.

-10^9 \leq C_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
P_1 P_2 \cdots P_N
C_1 C_2 \cdots C_N

Output

Print the maximum possible score at the end of the game.

Sample Input 1

5 2
2 4 5 1 3
3 4 -10 -8 8

Sample Output 1

8

When we start at some square of our choice and make at most two moves, we have the following options:

If we start at Square 1, making one move sends the piece to Square 2, after which the score is 4. Making another move sends the piece to Square 4, after which the score is 4 + (-8) = -4.

If we start at Square 2, making one move sends the piece to Square 4, after which the score is -8. Making another move sends the piece to Square 1, after which the score is -8 + 3 = -5.

If we start at Square 3, making one move sends the piece to Square 5, after which the score is 8. Making another move sends the piece to Square 3, after which the score is 8 + (-10) = -2.

If we start at Square 4, making one move sends the piece to Square 1, after which the score is 3. Making another move sends the piece to Square 2, after which the score is 3 + 4 = 7.

If we start at Square 5, making one move sends the piece to Square 3, after which the score is -10. Making another move sends the piece to Square 5, after which the score is -10 + 8 = -2.

The maximum score achieved is 8.

Sample Input 2

2 3
2 1
10 -7

Sample Output 2

13

Sample Input 3

3 3
3 1 2
-1000 -2000 -3000

Sample Output 3

-1000

We have to make at least one move.

Sample Input 4

10 58
9 1 6 7 8 4 3 2 10 5
695279662 988782657 -119067776 382975538 -151885171 -177220596 -169777795 37619092 389386780 980092719

Sample Output 4

29507023469

The absolute value of the answer may be enormous.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#include <cctype>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll          long long int
#define ld          long double
#define PRY         cout<<"YES\n"
#define PRN         cout<<"NO\n"
#define SETP(sp)    fixed<<setprecision(sp)
#define PB          push_back
#define GO          ll t; cin>>t; while(t--)
#define G(t)        cin>>t; while(t--)
#define ALLR(x)     x.rbegin(), x.rend()
#define PII         pair<ll ,ll >
#define VI          vector<ll >
#define ALL(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define PI          3.141592653589793238
#define SZ(x)       (ll )x.size()
#define hell9        998244353
#define hell        1000000007
#define FO(i,a,b)    for(i=a;i<b;i++)
#define FOREV(i,a,b)    for(i=a;i>=b;i--)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define BS          binary_search
#define MP          make_pair
#define LIM         100005
#define ARR(n,i,a)    ll n,i;cin>>n;ll a[n];FO(i,0,n)cin>>a[i];
#define ARRK(k,n,i,a) int n,k,i;scanf("%d%d",&n,&k);ll a[n];FO(i,0,n)scanf("%lld",&a[i]);
#define fio ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ord_set;
/*ll powM(ll x, ll y, ll p)
{
    ll res = 1;
    x = x % p;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1;
        x = (x * x) % p;
    }
    return res;
}/*
ll POW(ll x, ll y)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x);
        y = y >> 1;
        x = (x * x);
    }
    return res;
}
int LIS(std::vector<int>& v)
{
    if (v.size() == 0)
        return 0;
    std::vector<int> tail(v.size(), 0);
    int length = 1;
    tail[0] = v[0];
    for (int i = 1; i < v.size(); i++)
    {
        auto b = tail.begin(), e = tail.begin() + length;
        auto it = lower_bound(b, e, v[i]);
        if (it == tail.begin() + length)
            tail[length++] = v[i];
        else
            *it = v[i];
    }
    return length;
}
ll modInverse(ll n, ll p)
{
    return powM(n, p - 2, p);
}
/*
ll fac[500000] = {0};
ll done = 1;
ncrModP(ll n, ll r, ll p)
{
    if (r == 0)
        return 1;
 
    fac[0] = 1;
    if (n > done)
    {    for (ll i = done ; i <= n; i++)
            fac[i] = (fac[i - 1] * i) % p;
    }
    done = max(done, n);
 
    return (fac[n] * modInverse(fac[r], p) % p *
            modInverse(fac[n - r], p) % p) % p;
 
}
ll ncrM(ll n, ll r)
{
    if (r > n - r)
        r = n - r;
    ll C[r + 1];
    memset(C, 0, sizeof(C));
    C[0] = 1;
    for (ll i = 1; i <= n; i++) {
        for (ll j = min(i, r); j > 0; j--)
            C[j] = (C[j] + C[j - 1]);
    }
    return C[r];
}
map<ll, ll>f;
ll fib(ll k)
{
    ll i; f[0] = 1; f[1] = 1; f[2] = 2;
    if (k <= done)return f[k];
    for (i = done; i <= k; i++)f[i] = (f[i - 1] % hell + f[i - 2] % hell) % hell;
    done = k;
    return f[k] % hell;
}void sieve(int n)
{
     bool prime[5*LIM];
     memset(prime, true, sizeof(prime));
      int rootn = (int)sqrt(n);
     for (int p = 2; p <= rootn; p++)
          if (prime[p] == true)
               for (int i = p*p; i <= n; i += p)
                    prime[i] = false;
     prime[1] = 0;
 
}*/
bool sortinrev(const pair<int,int> &a,  
               const pair<int,int> &b) 
{ 
    if(a.first!=b.first)
       return (a.first > b.first); 
       else
       return (a.second<b.second);
}/*
int x[101];
int y[101];
bool vis[200005]={0};
int n;
int a[200005]={0};
vector<int> b[200005];
int p[200005]={0};

 vector<pair<int,int> > a1,a2;
 map<pair<int,int>,int >m;
 char gr[51][51];
 int n,c=0;
void dfs(int u,int v)
{
    if(u<0||u>=n||v<0||v>=n)return; 
    if(m[{u,v}]!=0)return;
    if(gr[u][v]=='1')return; 
    m[{u,v}]=c;
    if(c==1)a1.PB({u,v});
    if(c==2)a2.PB({u,v});
    dfs(u-1,v);
    dfs(u+1,v);
    dfs(u,v-1);
    dfs(u,v+1);
}*/
int t;
int main()
{
fio
ll n,k,i,fmax;
cin>>n>>k;
ll p[n],c[n];
FO(i,0,n){cin>>p[i];p[i]--;}
FO(i,0,n){cin>>c[i];if(i==0)fmax=c[0];else fmax=max(fmax,c[i]);}
bool vis[n+1];
if(fmax<=0){cout<<fmax;return 0;}
memset(vis,false,sizeof(vis));
vector<ll>v;
ll x,sum,ma,mb,mx,y,sumb,h,nv;
for(i=0;i<n;i++)
{
    if(!vis[i])
    {
        h=i;
        //v.PB(c[h]);
        while(!vis[h])
        {
            vis[h]=true;
            h=p[h];
            v.push_back(c[h]);
        }
        nv=v.size();
        FO(x,0,nv){v.PB(v[x]);}
        
        for(x=0;x<nv;x++)
        {
            sum=0;ma=0;mb=0;sumb=0;
            for(y=x;y<min((x+k),(x+nv));y++)
            {
                sum+=v[y];
                ma=max(ma,sum);
            }   
            if(sum>0&&k>nv)
            {
                mb=0;sumb=0;
                for(y=x;y<x+(k%(nv));y++)
                {
                    sumb+=v[y];
                    mb=max(sumb,mb);
                }
            }
            mx=max(max(ma,sum*(k/nv-1)+ma),sum*(k/(nv))+mb);
            fmax=max(fmax,mx);
          //  cout<<fmax<<"  "<<mx<<" "<<mb<<" "<<sumb<<" "<<ma<<" "<<sum<<" "<<nv<<"\n";
        }
        v.clear();
    }
}
cout<<fmax;
return 0;
}