//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
 
#include <x86intrin.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <assert.h>
#include <string>
#include <bitset> 
#include <vector>
#include <cmath>                                    
#include <deque>                                    
#include <queue>
#include <iomanip>
#include <stack>
#include <map>
#include <random>
#include <ctime>
#include <unordered_map>
#include <set>
#include <unordered_set>
//#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
//using namespace __gnu_pbds;
using namespace std;

 
//template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
     
#define F first
#define S second           
#define lb lower_bound               
#define ub upper_bound
#define pb push_back
#define pf push_front    
#define ppb pop_back
#define mp make_pair                                     
#define bpp __builtin_popcountll                                                                                        
#define sqr(x) ((x) * (x)) 
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define in insert
#define ppf pop_front
#define endl '\n'
//#define int long long
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
 
const int mod = (int)1e9 + 7;
const int N = (int)3e5 + 123;
const ll inf = (ll)1e18 + 1;
 
const double pi = acos (-1.0);
const double eps = 1e-9;                  
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

int n, k, p[N];
ll ans = -inf, c[N];

inline void boost () {
   ios_base :: sync_with_stdio (0);
   cin.tie (0), cout.tie (0);
}
 
inline void Solve () {
   cin >> n >> k;
   for (int i = 1; i <= n; i ++) cin >> p[i];
   for (int i = 1; i <= n; i ++) cin >> c[i];
   for (int i = 1; i <= n; i ++) {
      int nxt = p[i];
      ll cost = 0;
      int len = 0;
      ll mx = -inf;
      while (nxt != i) {
         if (len == k) break;
         len ++;
         cost += c[nxt];
         mx = max (mx, cost);
         ans = max (ans, cost);
         nxt = p[nxt];
      }
      if (len == k) {
         ans = max (ans, cost);
         continue;
      }
      len ++;
      cost += c[i];
      mx = max (mx, cost);
      ans = max (ans, cost);
      if (cost < 0) continue;
      ll how = k / len;
      ans = max (ans, cost * (how - 1) + mx);
      cost = cost * how;
      ans = max (ans, cost);
      int lef = k % len;
      if (lef > 0) {
         nxt = p[i];
         while (lef --) {
            cost += c[nxt];
            ans = max (ans, cost);
            nxt = p[nxt];
         }
         ans = max (ans, cost);
      }
      else ans = max (ans, cost);
   }
   cout << ans;
}               
 
signed main () {                                       
  // #ifdef QWERTY
   //   freopen("input", "r", stdin);
   //#endif
  // freopen (".in", "r", stdin);    
  // freopen (".out", "w", stdout);              
   boost ();
   int tt = 1;
   
   //cin >> tt;   
   while (tt --) {
      Solve ();
   }                                               
   return 0;      
}                   


Measured memory_bytes: 3696