Language: C++

#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi >
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii >
#define vpll vector<pll >
#define pb push_back
#define loop(i, s, l, j) for(int i = s; i < l; i += j)
#define rloop(i, s, l, j) for(int i = s; i >= l; i -= j)
#define rep(i, n) loop(i,0,n,1)
#define rrep(i, n) rloop(i,n-1,0,1) 
#define fastio() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define ft first
#define sd second   
#define mit mii::iterator
#define vit vi::iterator
#define all(a) a.begin(),a.end()
#define min3(a, b, c) min(a, min(b, c))
#define mod 998244353
#define inf 100000000000000000
#define limit 200005
#define minval -100000000000000000
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base,int exp){int ans=1;  while(exp > 0){if(exp&1)ans=(1LL*ans*base)%mod; exp=exp>>1; base=(1LL*base*base)%mod;}  return ans;}
/**********************************************************************************************************************************************/ 

void input()
{
	lli n, k;
	cin >> n >> k;
	vlli p(n);
	rep(i,n)
	{
		cin >> p[i];
		p[i]--;
	}

	vlli cost(n);
	rep(i,n)
		cin >> cost[i];

	vector<vlli > g(n);
	int sz = 0;

	vi vis(n, 0);
	rep(i,n)
	{
		if(vis[i])
			continue;
		
		sz++;
		g[sz-1].pb(cost[i]);
		vis[i] = 1;

		int j = p[i];
		while(j != i)
		{
			vis[j] = 1;
			g[sz-1].pb(cost[j]);
			j = p[j];
		}
	}

	lli ans = minval;
	rep(i,sz)
	{
		lli m = g[i].size();

		lli s1 = 0;
		rep(j,m)
			s1 += g[i][j];

		//complete rotations
		if(k/m >= 1)
		{
			
				rep(j,m)
				{
					lli s2_ = 0;
					rep(d,m)
					{
						s2_ += g[i][(j+d)%m];
						ans = max(ans, s2_);
					}
				}
			
			else
			{
				rep(j,m)
				{
					lli s2_ = 0;
					rep(d,k%m)
					{
						s2_ += g[i][(j+d)%m];
						ans = max(ans, (k/m)*s1 + s2_);
					}
				}
				ans = max(ans, (k/m) * s1);
			}
		}
		else
		{
			rep(j,m)
			{
				lli s2_ = 0;
				rep(d,k)
				{
					s2_ += g[i][(j+d)%m];
					ans = max(ans, s2_);
				}
			}
		}
	}
	cout << ans << '\n';
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
 
    fastio();
    int q = 1;
    //cin >> q;
    while(q--)
        input();
}

Compiler output:
a.cc: In function 'void input()':
a.cc:104:25: error: expected '}' before 'else'
  104 |                         else
      |                         ^~~~
a.cc:92:17: note: to match this '{'
   92 |                 {
      |                 ^
a.cc:118:17: error: 'else' without a previous 'if'
  118 |                 else
      |                 ^~~~
a.cc:120:31: error: 'm' was not declared in this scope
  120 |                         rep(j,m)
      |                               ^
a.cc:21:45: note: in definition of macro 'loop'
   21 | #define loop(i, s, l, j) for(int i = s; i < l; i += j)
      |                                             ^
a.cc:120:25: note: in expansion of macro 'rep'
  120 |                         rep(j,m)
      |                         ^~~
a.cc:125:50: error: 'i' was not declared in this scope
  125 |                                         s2_ += g[i][(j+d)%m];
      |                                                  ^
a.cc: At global scope:
a.cc:131:9: error: 'cout' does not name a type
  131 |         cout << ans << '\n';
      |         ^~~~
a.cc:132:1: error: expected declaration before '}' token
  132 | }
      | ^