void main() {
  problem();
}

void problem() {
  auto N = scan!int;
  auto K = scan!long;
  auto P = scan!int(N).map!(p => p - 1).array;
  auto C = scan!long(N);

  long solveStartFrom(int startPoint) {
    int point = P[startPoint];
    long[int] visited;
    visited[startPoint] = 0;

    long score = C[P[startPoint]];
    long maxScore = score;
    long[] scoreHistory = new long[N*2];

    for(long i = 1; i < K; i++) {
      if (point in visited) {
        // entry to loop
        auto loopSize = i - visited[point];
        auto loopScore = score - scoreHistory[i - loopSize];
        auto loopTimes = (K - i) / loopSize;
        ("---looped at " ~ i.to!string ~ " from " ~ visited[point].to!string ~ "---").deb;
        [score, scoreHistory[i - loopSize]].deb;
        [loopSize, loopScore, loopTimes].deb;
        i += loopSize * loopTimes - 1;
        score += loopScore * loopTimes;
        [score].deb;
        visited.clear;
      } else {
        visited[point] = i;
        point = P[point];
        score += C[point];
        if (i < N) scoreHistory[i] = score;
        if (maxScore < score) maxScore = score;
      }
    }

    return maxScore;
  }

  long solve() {
    long ans = long.min;
    foreach(i; 0..N) {
      ans = max(ans, solveStartFrom(i));
    }
    return ans;
  }

  solve().writeln;
}

// ----------------------------------------------

import std.stdio, std.conv, std.array, std.string, std.algorithm, std.container, std.range, core.stdc.stdlib, std.math, std.typecons, std.numeric;
T[][] combinations(T)(T[] s, in int m) {   if (!m) return [[]];   if (s.empty) return [];   return s[1 .. $].combinations(m - 1).map!(x => s[0] ~ x).array ~ s[1 .. $].combinations(m); }
string scan(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }
T scan(T)(){ return scan.to!T; }
T[] scan(T)(int n){ return n.iota.map!(i => scan!T()).array; }
void deb(T ...)(T t){ debug writeln(t); }
alias Point = Tuple!(long, "x", long, "y");

// -----------------------------------------------
