// created time: 2026-05-13 16:28:42
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define mkp make_pair
#define pb emplace_back
#define popcnt __builtin_popcountll
const int mod = 998244353;
inline ll read(){
	ll x=0, f=1; int ch=getchar();
	while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
	while(ch>='0' && ch<='9') x=x*10+ch-'0', ch=getchar();
	return x*f;
}
inline int lg2(int x){ return 31^__builtin_clz(x); }
inline ll lg2(ll x){ return 63^__builtin_clzll(x); }
template<typename T>inline void addmod(T &x){ if(x >= mod) x -= mod; }
template<typename T>inline void chkmax(T &a,T b){ a=max(a,b); }
template<typename T>inline void chkmin(T &a,T b){ a=min(a,b); }
inline ll qpow(ll a,ll b){
	ll ans=1, base=a;
	while(b){
		if(b&1) ans=ans*base%mod;
		base=base*base%mod; b>>=1;
	}
	return ans;
}
inline ll INV(ll x){ return qpow(x, mod-2); }

int n,a[400005],c[400005],rk[400005],r[400005],vis[400005],b[400005],m,k,h[400005];
ll sb[400005];
ll dp[400005];

int sh[400005];
int fk[400005],stk[400005],tp,prv[400005],jc[400005];

void procedure(){
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();	
		if(~a[i]) vis[a[i]]=1;
		h[i]=h[i-1]+(a[i]==-1);
	}
	for(int i=1;i<=n;i++)c[i]=read();
	for(int i=1;i<=n;i++)
		if(!vis[i]){
			rk[i]=1;
			sb[++m]=c[i];
		}
	for(int i=1;i<=m;i++)sb[i]+=sb[i-1];
	for(int i=1;i<=n;i++)rk[i]+=rk[i-1];
	for(int i=1;i<=n;i++)if(~a[i]) r[i]=rk[a[i]];

	int mx=0;
	for(int i=1;i<=n;i++){
		if(a[i]>mx && h[i]<=r[i]){
			mx=a[i];
			b[++k]=i;
		}
	}

	for(int i=1;i<=n;i++)sh[i]=h[i]-r[i];

	for(int I=1;I<=k;I++){
		while(tp && sh[b[stk[tp]]]>sh[b[I]]) tp--;
		prv[I]=stk[tp];
		stk[++tp]=I;
	}

	// solve(1,k,0,k-1);
#define i b[I]
#define j b[J]
	for(int I=1;I<=k;I++){
		dp[i]=-1e18;
		for(int J=prv[I]+1;J<I;J++)
			chkmax(dp[i], dp[j]-sb[r[i]-h[i]+h[j]]);

		int J=prv[I];
		chkmax(dp[i], dp[j]-sb[max(r[j],r[i]-(h[i]-h[j]))]);

		dp[i]+=sb[r[i]]+c[a[i]];
	}
#undef i
#undef j

	for(int i=1;i<=k;i++)assert(jc[i]>=jc[i-1]);


	for(int i=1;i<=n;i++){
		ll ans=0;
		for(int J=k;J>=0;J--){
			int j=b[J];
			if(j>i)continue;
			chkmax(ans, dp[j]+sb[m]-sb[max(r[j],m-(h[i]-h[j]))]);
			if(h[i]-h[j]>=m-r[j]) break;
		}
		printf("%lld ",ans);
	}
	puts("");
}
int main(){
	#ifdef LOCAL
		assert(freopen("test.in","r",stdin));
		assert(freopen("test.out","w",stdout));
	#endif
	ll T=1;
	// math_init();
	while(T--) procedure();
	return 0;
}
← ↑