Language: C++

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>


using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int n;
ll k;
int slika[5005];
ll cost[5005];
bool bio[5005];
vector<ll> tr;


int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    ff(i,1,n){
        cin >> slika[i];
    }
    ff(i,1,n){
        cin >> cost[i];
    }
    ll ans = -1e9;
    ff(i,1,n){
        if(bio[i])continue;
        int pom = i;
        bio[i] = 1;
        tr.clear();
        while(1){
            tr.pb(cost[pom]);
            pom = slika[pom];
            if(bio[pom])break;
            bio[pom] = 1;
        }
        ll tmp = tr[0];
        ll m = tr.size();
        ff(j,0,m - 1){
            ll sum = 0;
            ff(r,j + 1, j + min(m - 1,k)){
                sum += tr[r%m];
                tmp = max(tmp, sum);
            }
        }
        ll s1 = 0;
        ff(j,0,m - 1)s1 += tr[j];
        ll novok = k%m;
        s1 *= (k/m);
        ff(j,0,m - 1){
            ll sum = 0;
            ff(r,j+1, j + min(m-1,novok)){
                sum += tr[r%m];
                tmp = max(tmp, sum + s1);
            }
        }
        ans = max(ans, tmp);
    }
    cout << ans << "\n";
    return 0;
}
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>


using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

int n;
ll k;
int slika[5005];
ll cost[5005];
bool bio[5005];
vector<ll> tr;


int main()
{

    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n >> k;
    ff(i,1,n){
        cin >> slika[i];
    }
    ff(i,1,n){
        cin >> cost[i];
    }
    ll ans = -1e9;
    ff(i,1,n){
        if(bio[i])continue;
        int pom = i;
        bio[i] = 1;
        tr.clear();
        while(1){
            tr.pb(cost[pom]);
            pom = slika[pom];
            if(bio[pom])break;
            bio[pom] = 1;
        }
        ll tmp = tr[0];
        ll m = tr.size();
        ff(j,0,m - 1){
            ll sum = 0;
            ff(r,j + 1, j + min(m - 1,k)){
                sum += tr[r%m];
                tmp = max(tmp, sum);
            }
        }
        ll s1 = 0;
        ff(j,0,m - 1)s1 += tr[j];
        ll novok = k%m;
        s1 *= (k/m);
        ff(j,0,m - 1){
            ll sum = 0;
            ff(r,j+1, j + min(m-1,novok)){
                sum += tr[r%m];
                tmp = max(tmp, sum + s1);
            }
        }
        ans = max(ans, tmp);
    }
    cout << ans << "\n";
    return 0;
}


Compiler output:
a.cc:97:9: error: redefinition of 'std::mt19937 rnd'
   97 | mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
      |         ^~~
a.cc:20:9: note: 'std::mt19937 rnd' previously declared here
   20 | mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
      |         ^~~
a.cc:99:5: error: redefinition of 'int n'
   99 | int n;
      |     ^
a.cc:22:5: note: 'int n' previously declared here
   22 | int n;
      |     ^
a.cc:100:4: error: redefinition of 'long long int k'
  100 | ll k;
      |    ^
a.cc:23:4: note: 'long long int k' previously declared here
   23 | ll k;
      |    ^
a.cc:101:5: error: redefinition of 'int slika [5005]'
  101 | int slika[5005];
      |     ^~~~~
a.cc:24:5: note: 'int slika [5005]' previously declared here
   24 | int slika[5005];
      |     ^~~~~
a.cc:102:4: error: redefinition of 'long long int cost [5005]'
  102 | ll cost[5005];
      |    ^~~~
a.cc:25:4: note: 'long long int cost [5005]' previously declared here
   25 | ll cost[5005];
      |    ^~~~
a.cc:103:6: error: redefinition of 'bool bio [5005]'
  103 | bool bio[5005];
      |      ^~~
a.cc:26:6: note: 'bool bio [5005]' previously declared here
   26 | bool bio[5005];
      |      ^~~
a.cc:104:12: error: redefinition of 'std::vector<long long int> tr'
  104 | vector<ll> tr;
      |            ^~
a.cc:27:12: note: 'std::vector<long long int> tr' previously declared here
   27 | vector<ll> tr;
      |            ^~
a.cc:107:5: error: redefinition of 'int main()'
  107 | int main()
      |     ^~~~
a.cc:30:5: note: 'int main()' previously defined here
   30 | int main()
      |     ^~~~