#include "bits/stdc++.h"

using namespace std;

vector<long long> parents;
vector<long long> ranks;

void init(long long n) {
    parents = vector<long long>(n, 0);
    ranks = vector<long long>(n, 0);
    for (long long i = 0; i < n; ++i) {
        parents[i] = i;
    }
}

long long find(long long x) {
    if (parents[x] == x) {
        return x;
    }
    else {
        parents[x] = find(parents[x]);
        return parents[x];
    }
}

void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) {
        return;
    }

    if (ranks[x] < ranks[y]) {
        parents[x] = y;
    }
    else if (ranks[x] > ranks[y]) {
        parents[y] = x;
    }
    else {
        parents[y] = x;
        ++ranks[x];
    }
}

bool same(long long x, long long y) {
    return find(x) == find(y);
}

void Main() {
    long long N, K;
    cin >> N >> K;
    vector<long long> P(N, 0LL);
    vector<long long> C(N, 0LL);
    for (long long i = 0; i < N; ++i) {
        cin >> P[i];
        --P[i];
    }
    for (long long i = 0; i < N; ++i) {
        cin >> C[i];
    }

    vector<vector<long long>> cyclics(N, vector<long long>());
    for (long long i = 0; i < N; ++i) {
        init(N);
        cyclics[i].push_back(i);
        long long prev = i;
        long long next = P[i];
        while (!same(next, prev)) {
            unite(prev, next);
            cyclics[i].push_back(next);
            prev = next;
            next = P[prev];
        }
    }

    vector<long long> roundTripPts(N, 0LL);
    vector<long long> maxOfRoundTrip(N, -5000LL * 1000000001LL);
    vector<long long> numForRoundTrip(N, 0LL);
    vector<long long> maxAchieveIndexes(N, 0LL);
    for (long long i = 0; i < N; ++i) {
        numForRoundTrip[i] = cyclics[i].size();
        for (long long j = 0; j < cyclics[i].size(); ++j) {
            roundTripPts[i] += C[cyclics[i][j]];
            if (maxOfRoundTrip[i] < roundTripPts[i]) {
                maxOfRoundTrip[i] = roundTripPts[i];
                maxAchieveIndexes[i] = j;
            }
        }
    }

    vector<long long> maxPts(N, 0LL);
    for (long long i = 0; i < N; ++i) {
        if (K <= numForRoundTrip[i]) {
            long long temp = 0LL;
            long long tempMax = 0LL;
            for (long long j = 0; j < K; ++j) {
                temp += C[cyclics[i][j]];
                tempMax = max(tempMax, temp);
            }
            if (tempMax == 0LL) {
                maxPts[i] = C[i];
            }
            else {
                maxPts[i] = tempMax;
            }
        }
        else if (roundTripPts[i] < 0) {
            maxPts[i] = maxOfRoundTrip[i];
        }
        else {
            long long temp = 0LL;
            long long tempMax = 0LL;
            long long loopMax = (K % numForRoundTrip[i]) + numForRoundTrip[i];
            for (long long j = 0; j < loopMax; ++j) {
                temp += C[cyclics[i][j % numForRoundTrip[i]]];
                tempMax = max(tempMax, temp);
            }
            maxPts[i] = roundTripPts[i] * (K / numForRoundTrip[i] - 1) + tempMax;
        }
    }

    cout << *max_element(maxPts.begin(), maxPts.end()) << endl;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 219284