#include <bits/stdc++.h>
using namespace std;
long long n,k;
long long a[100002],s[100002],p[35][100002];
long long cnt[10005];
long long cal(long long x,long long y) {
for (int i = 0; i < 30; i++) {
if ((y >> i)&1){
x = p[i][x];
}
}
return x;
}
int main() {
ios::sync_with_stdio(0);
cin.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];
p[0][i] = s[i];
}
// total[i][j]: sau 2^i lan nem thi j co bao nhieu bong
for (int i = 1; i <= 30; i++) {
for (int j = 1; j <= n; j++) {
p[i][j] = p[i-1][p[i-1][j]];
}
}
for (int i= 1; i <= n; i++) {
cnt[cal(i,k)] += a[i];
}
long long maxx = 0;
for (int i = 1; i <= n; i++) maxx = max(maxx,cnt[i]);
cout << maxx << "\n";
for (int i = 1; i <= n; i++) if (maxx == cnt[i]) cout << i << " ";
}