Language: C++

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <complex>
#include <bitset>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <chrono>
#include <random>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v) rep(i, (v).size() - 1) cout << (v)[i] << ' '; cout << (v)[n - 1] << '\n';
#ifdef ONLINE_JUDGE
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define dbg(v)
#else
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
#endif

// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;

template<typename T> T gcd(T a, T b) {
    if (a < b) return gcd(b, a);
    T r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

template<typename T> T lcm(const T a, const T b) {
    return a / gcd(a, b) * b;
}

template<typename T> bool chmin(T& a, const T& b) {
    if (a > b) return a = b, true; else return false;
}

template<typename T> bool chmax(T& a, const T& b) {
    if (a < b) return a = b, true; else return false;
}

// End of template.

int main() {
    cout << fixed << setprecision(15);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);

    int64 n, k, p[5050], c[5050];
    cin >> n >> k;
    rep(i, n) cin >> p[i], p[i]--;
    rep(i, n) cin >> c[i];

    int64 ans = -INF64;
    rep(s, n) {
        int64 dp[5050];
        int x = s;
        int cnt = 0;
        dp[0] = 0;
        for (cnt = 0;; ++cnt) {
            x = p[x];
            dp[cnt + 1] = dp[cnt] + c[x];
            if (x == s) break;
        }
        cnt += 1;
        int64 t = -INF64;
        for (int i = 1; i <= min(n, k); ++i) chmax(t, dp[i] + max(0LL, dp[cnt] * ((k - i) / cnt)));
        chmax(ans, t);
    }

    print(ans);

    return 0;
}

/*
     _
  __| |  ___ _ __  _ __
 / _` | / __| '_ \| '_ \
| (_| || (__| |_) | |_) |
 \__,_(_)___| .__/| .__/
            |_|   |_|
*/
/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <complex>
#include <bitset>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <chrono>
#include <random>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v) rep(i, (v).size() - 1) cout << (v)[i] << ' '; cout << (v)[n - 1] << '\n';
#ifdef ONLINE_JUDGE
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define dbg(v)
#else
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
#endif

// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;

template<typename T> T gcd(T a, T b) {
    if (a < b) return gcd(b, a);
    T r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

template<typename T> T lcm(const T a, const T b) {
    return a / gcd(a, b) * b;
}

template<typename T> bool chmin(T& a, const T& b) {
    if (a > b) return a = b, true; else return false;
}

template<typename T> bool chmax(T& a, const T& b) {
    if (a < b) return a = b, true; else return false;
}

// End of template.

int main() {
    cout << fixed << setprecision(15);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);

    int64 n, k, p[5050], c[5050];
    cin >> n >> k;
    rep(i, n) cin >> p[i], p[i]--;
    rep(i, n) cin >> c[i];

    int64 ans = -INF64;
    rep(s, n) {
        int64 dp[5050];
        int64 x = s;
        int64 cnt = 0;
        dp[0] = 0;
        for (cnt = 0;; ++cnt) {
            x = p[x];
            dp[cnt + 1] = dp[cnt] + c[x];
            if (x == s) break;
        }
        cnt += 1;
        int64 t = -INF64;
        for (int64 i = 1; i <= min(n, k); ++i) chmax(t, dp[i] + max(0LL, dp[cnt] * ((k - i) / cnt)));
        chmax(ans, t);
    }

    print(ans);

    return 0;
}

/*
     _
  __| |  ___ _ __  _ __
 / _` | / __| '_ \| '_ \
| (_| || (__| |_) | |_) |
 \__,_(_)___| .__/| .__/
            |_|   |_|
*/


Compiler output:
a.cc:220:11: error: redefinition of 'const int INF'
  220 | const int INF = (1<<30) - 1;
      |           ^~~
a.cc:76:11: note: 'const int INF' previously defined here
   76 | const int INF = (1<<30) - 1;
      |           ^~~
a.cc:221:13: error: redefinition of 'const int64 INF64'
  221 | const int64 INF64 = (1LL<<62) - 1;
      |             ^~~~~
a.cc:77:13: note: 'const int64 INF64' previously defined here
   77 | const int64 INF64 = (1LL<<62) - 1;
      |             ^~~~~
a.cc:223:24: error: redefinition of 'template<class T> T gcd(T, T)'
  223 | template<typename T> T gcd(T a, T b) {
      |                        ^~~
a.cc:79:24: note: 'template<class T> T gcd(T, T)' previously declared here
   79 | template<typename T> T gcd(T a, T b) {
      |                        ^~~
a.cc:233:24: error: redefinition of 'template<class T> T lcm(T, T)'
  233 | template<typename T> T lcm(const T a, const T b) {
      |                        ^~~
a.cc:89:24: note: 'template<class T> T lcm(T, T)' previously declared here
   89 | template<typename T> T lcm(const T a, const T b) {
      |                        ^~~
a.cc:237:27: error: redefinition of 'template<class T> bool chmin(T&, const T&)'
  237 | template<typename T> bool chmin(T& a, const T& b) {
      |                           ^~~~~
a.cc:93:27: note: 'template<class T> bool chmin(T&, const T&)' previously declared here
   93 | template<typename T> bool chmin(T& a, const T& b) {
      |                           ^~~~~
a.cc:241:27: error: redefinition of 'template<class T> bool chmax(T&, const T&)'
  241 | template<typename T> bool chmax(T& a, const T& b) {
      |                           ^~~~~
a.cc:97:27: note: 'template<class T> bool chmax(T&, const T&)' previously declared here
   97 | template<typename T> bool chmax(T& a, const T& b) {
      |                           ^~~~~
a.cc:247:5: error: redefinition of 'int main()'
  247 | int main() {
      |     ^~~~
a.cc:103:5: note: 'int main()' previously defined here
  103 | int main() {
      |     ^~~~