#include<stdio.h>
#include<set>
#include<vector>
#include<queue>

using namespace std;

const int MAXN=100000;
typedef pair<int,int> ip;

int a[MAXN];
int b[MAXN];
int n,k;
vector<int> g[MAXN];
set<ip> s;


void sredi(int i, int x)
{
          queue<int> q;
          q.push(i);
          while(true)
          {
                     
                     ip p;
                     p.first=i;
                     p.second=a[i];
                     s.erase(p);
                     a[i]-=x;
                     if(a[i]<=k && a[i]>0)
                     {
                                p.second=a[i];
                                s.erase(p);           
                     }
                     if(i==0) break;
                     i=b[i];
          }
          while(!q.empty())
          {
                           i=q.front();
                           q.pop();
                           ip p;
                           p.first=a[i];
                           p.second=i;
                           s.erase(p);
                           a[i]=0;
                           for(int j=0; j<g[i].size(); j++) q.push(g[i][j]);                 
          }
}




int main()
{
    FILE *_f,*_g;
    _f=fopen("porez.in","r");
    _g=fopen("porez.out","w");
    fscanf(_f,"%d %d",&n,&k);
    for(int i=0; i<n; i++) fscanf(_f,"%d",&a[i]);
    for(int i=1; i<n; i++) {fscanf(_f,"%d",&b[i]);b[i]--;}
    for(int i=n-1; i>0; i--)
    {
            a[b[i]]+=a[i];
            g[b[i]].push_back(i);
    }
    
    for(int i=0; i<n; i++)
    {
            if(a[i]<=k) 
            {
                        ip p;
                        p.first=a[i];
                        p.second=i;
                        s.insert(p);            
            } 
    }
    /*for(set<ip>::iterator i=s.begin(); i!=s.end(); i++) 
    {
                          ip p;
                          p=*i;
                          printf("%d\n",p.first);
    }*/
    int resenje=0;
    while(a[0]>k)
    {
                 ip p;
                 set<ip>::iterator it;
                 it=s.end();
                 it--;
                 p=*it;
                 int i=p.second;
                 sredi(i,p.first);
                 printf("%d\n",p.first);
                 resenje++;
    }
    
    fprintf(_g,"%d",resenje+1);
    
    
    
    
    
    
  // scanf("%d",&n);
    
    fclose(_f);
    fclose(_g);
}
