#include <iostream>
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int N = 1e5 + 5 ;
int a[N] , s[N] , n , k ;
namespace sub1
{
int a_new[N] ;
void solve()
{
while(k -- )
{
for(int i = 1 ; i <= n ; i ++)
{
a_new[s[i]] += a[i] ;
}
for(int i = 1 ; i <= n ; i ++ )
{
a[i] += a_new[i] - a[i] ;
a_new[i] = 0 ;
}
}
int maxi = 0 ;
for(int i = 1 ; i<= n ; i ++)
{
maxi = max(maxi , a[i]) ;
}
cout<<maxi<<endl ;
for(int i = 1 ; i <= n ; i ++)
{
if(a[i] == maxi) cout<<i<<" " ;
}
}
}
bool check_sub2()
{
map<int,int> cnt ;
for(int i = 1 ; i <= n ; i ++)
{
cnt[s[i]] ++;
}
for(auto e : cnt)
{
if(cnt[e.first] >= 2)
{
return false ;
}
}
return true ;
}
namespace sub2
{
int tin[N] , low[N] , t = 0 , cnt = 0 , group[N] , pos[N] ;
stack<int> st ;
vector<int> g[N] ;
vector<int> luu[N] ;
bool out_stack[N] ;
void dfs(int u)
{
t ++ ;
tin[u] = low[u] = t ;
st.push(u) ;
for(int v : g[u])
{
if(out_stack[v] == false)
{
if(low[v] != 0)
{
low[u] = min(low[u] , tin[v]) ;
}
else
{
dfs(v) ;
low[u] = min(low[u] , low[v]) ;
}
}
}
if(low[u] == tin[u])
{
int v ;
cnt ++ ;
do
{
v = st.top() ;
st.pop();
out_stack[v] = true ;
luu[cnt].push_back(v) ;
group[v] = cnt ;
// cout<<v<<" " ;
}while(u != v);
// cout<<endl ;
}
}
int a_new[N] ;
void solve()
{
for(int i = 1 ; i <= n ; i ++ )
{
g[i].push_back(s[i]) ;
}
for(int i = 1 ; i <= n ; i ++ )
{
if(tin[i] == 0) dfs(i) ;
}
for(int i = 1 ; i <= cnt ; i ++)
{
reverse(luu[i].begin() , luu[i].end()) ;
for(int j = 0 ; j <= luu[i].size() - 1 ; j ++ )
{
pos[luu[i][j]] = j ;
}
}
for(int i = 1 ; i <= n ; i ++ )
{
int next = luu[group[i]][(pos[i] + k) % luu[group[i]].size()] ;
a_new[next] += a[i] ;
}
for(int i = 1 ; i <= n ; i ++ )
{
a[i] += a_new[i] - a[i] ;
}
int maxi = 0 ;
for(int i = 1 ; i <= n ; i ++ )
{
maxi = max(maxi , a[i]) ;
}
cout<<maxi<<endl;
for(int i = 1 ; i <= n ; i ++ )
{
if(a[i] == maxi)
{
cout<<i<<" ";
}
}
}
}
namespace sub3
{
int next[N][31] ;
void intit()
{
for(int i = 1 ; i <= 30 ; i ++)
{
for(int u = 1 ; u <= n ; u ++ )
{
next[u][i] = next[next[u][i - 1]][i - 1] ;
}
}
}
int a_new[N] ;
void solve()
{
for(int u = 1 ; u <= n ; u ++)
{
next[u][0] = s[u] ;
}
intit() ;
for(int u = 1 ; u <= n ; u ++)
{
int tmp = k ;
int tieptheo = u ;
for(int i = 30 ; i >= 0 ; i -- )
{
if(tmp >= (int)(1<<i))
{
tmp -= (int)(1<<i) ;
tieptheo = next[tieptheo][i] ;
}
}
a_new[tieptheo] += a[u] ;
}
for(int i = 1 ; i <= n ; i ++)
{
a[i] += a_new[i] - a[i] ;
}
int maxi = 0 ;
for(int i = 1 ; i <= n ; i ++)
{
maxi = max(maxi , a[i]) ;
}
cout<<maxi<<endl ;
for(int i = 1 ; i <= n ; i ++ )
{
if(a[i] == maxi) cout<<i<<" " ;
}
}
}
signed main()
{
ios::sync_with_stdio(0) ;
cin.tie(0) ; cout.tie(0) ;
cin>>n>>k;
for(int i = 1 ; i <= n ; i ++ )
{
cin>>a[i] ;
}
for(int i = 1 ; i <= n ; i ++ )
{
cin>>s[i] ;
}
//
if(n <=1000 && k <= 1000) return sub1::solve() , 0 ;
if(check_sub2())return sub2::solve() , 0 ;
return sub3::solve() , 0 ;
return 0;
}