#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int n, k, i, j, x, p[5005], c[5005];
i64 ans = numeric_limits<i64>::min(), a[5005], b[5005];
int main() {
    scanf("%d%d", &n, &k);
    for (i = 1; i <= n; ++i)
        scanf("%d", &p[i]);
    for (i = 1; i <= n; ++i)
        scanf("%d", &c[i]);
    for (i = 1; i <= n; ++i) {
        for (j = 1, x = p[i]; j <= k; ++j, x = p[x]) {
            a[j] = a[j - 1] + c[x];
            b[j] = max(b[j - 1], a[j]);
            ans = max(ans, a[j]);
            if (j == k)
                break;
            if (x == i) {
                ans = max(ans, a[j] * (k / j) + b[k % j]);
                ans = max(ans, a[j] * (k / j - 1) + b[j]);
                break;
            }
        }
    }
    printf("%lld", ans);
}