#include <iostream>
#include <iomanip>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <array>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <numeric>
#include <bitset>
#include <cstdint>
#include <stdexcept>
using namespace std;

// 整数型

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

// [0,n)を昇順/降順でループ

#define rep(i, n) for (int i = 0; i < static_cast<int>(n); ++i)
#define repd(i, n) for (int i = static_cast<int>(n) - 1; i >= 0; --i)

// [a,b)を昇順/降順でループ

#define FOR(i, a, b) for (int i = static_cast<int>(a); i < static_cast<int>(b); ++i)
#define FORD(i, a, b) for (int i = static_cast<int>(b) - 1; i >= static_cast<int>(a); --i)

// デバッグ

#ifdef DEBUG
#include <cassert>
#define my_assert(x) assert(x)
#else
#define my_assert(x)
#define assert(x)
#endif

//////////////// //////////////// //////////////// //////////////// ////////////////
//////////////// //////////////// //////////////// //////////////// ////////////////

int main()
{
    // 高速な入出力
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // 浮動小数の表示の精度
    cout << setprecision(15);

    int n, k;
    cin >> n >> k;
    vector<int> perm(n);
    rep(i, n)
    {
        cin >> perm[i];
        --perm[i];
    }
    vector<i64> masu(n);
    rep(i, n)
    {
        cin >> masu[i];
    }

    vector<int> hd(n, -1);
    vector<vector<int>> orbit(n);
    rep(i, n)
    {
        if (hd[i] >= 0)
        {
            continue;
        }
        orbit[i] = {i};
        hd[i] = i;
        int j = perm[i];
        while (j != i)
        {
            orbit[i].push_back(j);
            hd[j] = i;
            j = perm[j];
        }
    }
    vector<vector<i64>> score2(n);
    rep(i, n)
    {
        if (hd[i] < i)
        {
            continue;
        }
        score2[i].resize(3 * orbit[i].size() + 1);
        rep(j, orbit[i].size())
        {
            score2[i][j + 1] = score2[i][j] + masu[orbit[i][j]];
        }
        rep(j, orbit[i].size())
        {
            score2[i][j + 1 + orbit[i].size()] = score2[i][orbit[i].size()] + score2[i][j + 1];
        }
        rep(j, orbit[i].size())
        {
            score2[i][j + 1 + 2 * orbit[i].size()] = 2 * score2[i][orbit[i].size()] + score2[i][j + 1];
        }
    }

    // hd[i] == i ごとに最大値を求める
    i64 ans = INT64_MIN;
    rep(i, n)
    {
        if (hd[i] < i)
        {
            continue;
        }
        int tmpk = k;
        i64 offset = 0;
        i64 res = INT64_MIN;
        if (score2[i][orbit[i].size()] >= 0)
        {
            offset = score2[i][orbit[i].size()] * max(0, tmpk / (int)orbit[i].size() - 1);
            tmpk = tmpk - max(0, tmpk / (int)orbit[i].size() - 1) * (int)orbit[i].size();
            if (tmpk < k)
            {
                res = 0;
            }
        }
        else
        {
            tmpk = orbit[i].size();
        }

        rep(pt1, orbit[i].size())
        {
            // pt1 + lh まで
            // 0 < lh <= tmpk < 2 * orbit[i].size()
            FOR(lh, 1, tmpk + 1)
            {
                res = max(res, score2[i][pt1 + lh] - score2[i][pt1]);
            }
        }
        ans = max(ans, res + offset);
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 4064