#include<bits/stdc++.h>usingnamespace std;intmain(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);int n, m;
cin >> n >> m;
vector<vector<int>>vec(m +1,vector<int>(2,0));
unordered_set<int> mset;for(int i =1; i <= m; i++){
cin >> vec[i][0];
mset.insert(i);}for(int i =1; i <= m; i++){
cin >> vec[i][1];if(mset.find(vec[i][0])!= mset.end()){
mset.erase(vec[i][0]);}}
vector<int>early(m +1,0);//最早
vector<int>late(m +1, INT_MAX);//最晚for(int i =1; i <= m; i++){int day =1;int j = vec[i][0];while(j !=0){
day += vec[j][1];
j = vec[j][0];}
early[i]= day;
cout << day <<" ";}
cout << endl;for(auto it : mset){
late[it]= n - vec[it][1]+1;int i = it;int j = vec[it][0];//j为i的上一个节点while(j !=0){
late[j]=min(late[j], late[i]- vec[j][1]);//可能有不同路径,所以要取最小值
i = j;
j = vec[j][0];}}int flag =1;for(int i =1; i <= m; i++){if(late[i]<=0)
flag =0;}if(flag){for(int i =1; i <= m; i++){
cout << late[i]<<" ";}}return0;}