Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5007;

ll n, k, p[N], c[N], sum[N];

int vis[N];

vector<ll>g[N];

ll tree[8 * N];

#define m (l + r) / 2
#define lson 2 * node
#define rson 2 * node + 1

void update(ll v, int pos, int l, int r, int node) {
    if (l == r) {
        tree[node] = v;
        return;
    }
    if (pos <= m) update(v, pos, l, m, lson);
    else update(v, pos, m + 1, r, rson);
    tree[node] = min(tree[lson], tree[rson]);

}


ll query(int ql, int qr, int l, int r, int node) {
    if (ql <= l && qr >= r) {
        return tree[node];
    }
    ll ans = LLONG_MAX;
    if (ql <= m) ans = min(ans, query(ql, qr, l, m, lson));
    if (qr > m) ans = min(ans, query(ql, qr, m + 1, r, rson));
    return ans;
}

ll work(int x, int cnt) {
    for (int i = 1; i <= x; i++) {
        update(sum[i], i, 1, x, 1);
    }
    ll mx1 = LLONG_MIN;
    for (int i = 1; i <= x; i++) {
        int l =max(1, i - cnt + 1);
        if (i - 1 >= l) {
             ll res = query(l, i - 1, 1, x, 1);
            mx1 = max(mx1, sum[i] - res);
        } else {
            mx1 = max(mx1, 0);
        }
       
        mx1 = max(mx1, sum[i] - sum[max(0, i - cnt)]);
        
    }
    return mx1;
}

void dfs(int id, int u) {
    vis[u] = 1;
    g[id].push_back(u);
    if (vis[p[u]] == 0) {
        dfs(id, p[u]);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    int top = 1;
    for (int i = 1; i <= n; i++) {
        if (vis[i] == 0) {
            dfs(top, i);
            top++;
        }
    }
    ll maxn = LLONG_MIN;
    for (int i = 1; i < top; i++) {
        ll cnt = 0;
        for (int j = 0; j < g[i].size(); j++) {
            cnt += c[g[i][j]];
            sum[j + 1] = c[g[i][j]];
        }
    
        int cn = (int)g[i].size();
        for (int j = 1; j <= cn; j++) {
            sum[j + cn] = sum[j];
        }
        for (int j = 1; j <= 2 * cn; j++) {
            sum[j] = sum[j - 1] + sum[j];
        }
        if (cnt > 0) {
            ll res = k / cn;
            ll cat = res * cnt;
            res = k % cn;
            ll ca = cat;
            if (res) {
                cat += work(2 * cn, res);
                maxn = max(maxn, cat);
            } 
            cat = ca;
            cat -= cnt;
            cat += work(2 * cn, cn);
            maxn= max(maxn, cat);
            
            
        } 
        if (k >= cn) {
            maxn = max(maxn, work(2 * cn, cn));
        } else {
            maxn = max(maxn, work(2 * cn, k));
        }

    }
    cout << maxn << endl;
}


/*9 7876
4 2 5 1 6 8 3 7 9 
-6557 -2648 -2420 25613 4314 25455 -6418 -2334 -763 */
/*

3 1670
1 3 2 
-10082 20147 -12332 
*/

Compiler output:
a.cc: In function 'll work(int, int)':
a.cc:51:22: error: no matching function for call to 'max(ll&, int)'
   51 |             mx1 = max(mx1, 0);
      |                   ~~~^~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:51:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   51 |             mx1 = max(mx1, 0);
      |                   ~~~^~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:51:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   51 |             mx1 = max(mx1, 0);
      |                   ~~~^~~~~~~~