#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>> score(n);
    rep(i, n)
    {
        if (hd[i] < i)
        {
            continue;
        }
        score[i].resize(orbit[i].size() + 1);
        rep(j, orbit[i].size())
        {
            score[i][j + 1] = score[i][j] + masu[orbit[i][j]];
        }
#ifdef DEBUG
        clog << "i " << i << ", size " << orbit[i].size() << endl;
#endif
    }

    // hd[i] == i ごとに最大値を求める
    i64 ans = INT64_MIN;
    rep(i, n)
    {
        if (hd[i] < i)
        {
            continue;
        }
        i64 tmpans = 0; // 下駄
        i64 tmpans2 = INT64_MIN;
        int tmpk = k;
        if (*score[i].rbegin() <= 0)
        {
            tmpk = min((size_t)tmpk, orbit[i].size() - 1);
        }
        else
        {
            tmpans += (*score[i].rbegin()) * (tmpk / orbit[i].size());
            tmpk = tmpk % orbit[i].size();
            if (tmpk < k)
            {
                tmpans2 = 0;
            }
        }
        // 総当り tmpk個まで選ぶ
        rep(pt1, orbit[i].size())
        {
            // pt1 -> pt2
            for (int lh = 1; pt1 + lh <= (int)orbit[i].size(); ++lh)
            {
                int pt2 = pt1 + lh;
                if (lh <= tmpk)
                {
                    // [pt1, pt1+lh)
                    tmpans2 = max(tmpans2, score[i][pt2] - score[i][pt1]);
                }
                if (0 < orbit[i].size() - lh and (int) orbit[i].size() - lh <= tmpk)
                {
                    // [pt1+lh, pt1)
                    tmpans2 = max(tmpans2, score[i][pt1] + *score[i].rbegin() - score[i][pt2]);
                }
            }
        }
        ans = max(ans, tmpans + tmpans2);

        if (*score[i].rbegin() > 0 and k >= orbit[i].size())
        {
            tmpans = (*score[i].rbegin()) * (k / orbit[i].size() - 1);
            tmpk = orbit[i].size() - 1;
            tmpans2 = 0;
            rep(pt1, orbit[i].size())
            {
                // pt1 -> pt2
                for (int lh = 1; pt1 + lh <= (int)orbit[i].size(); ++lh)
                {
                    int pt2 = pt1 + lh;
                    if (lh <= tmpk)
                    {
                        // [pt1, pt1+lh)
                        tmpans2 = max(tmpans2, score[i][pt2] - score[i][pt1]);
                    }
                    if (0 < orbit[i].size() - lh and (int) orbit[i].size() - lh <= tmpk)
                    {
                        // [pt1+lh, pt1)
                        tmpans2 = max(tmpans2, score[i][pt1] + *score[i].rbegin() - score[i][pt2]);
                    }
                }
            }
            ans = max(ans, tmpans + tmpans2);
        }
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 4008