This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#define N	400000
long long max(long long a, long long b) { return a > b ? a : b; }
long long ss[N + 1];
int xx[N], yy[N], dd[N]; long long dp[N]; int pp[N], qq[N], hh[N], tt[N];
int n, m;
int cross(int i, int j) {
	int lower = -1, upper = m - yy[j] + 1;
	while (upper - lower > 1) {
		int d = (lower + upper) / 2;
		if (dp[i] - ss[yy[i] + d] > dp[j] - ss[yy[j] + d])
			upper = d;
		else
			lower = d;
	}
	return lower;
}
void merge(int i, int j) {
	int u, v, d;
	u = tt[i], v = hh[j];
	while (1) {
		d = cross(u, v);
		if (u != hh[i] && cross(pp[u], u) >= d)
			u = pp[u];
		else if (v != tt[j] && cross(v, qq[v]) <= d)
			v = qq[v];
		else {
			qq[u] = v, pp[v] = u;
			break;
		}
	}
	hh[j] = hh[i];
}
long long query(int i, int d) {
	while (hh[i] != tt[i] && cross(hh[i], qq[hh[i]]) <= d)
		hh[i] = qq[hh[i]];
	return dp[hh[i]] - ss[yy[hh[i]] + d];
}
int main() {
	static int aa[N], ii[N], ww[N], qu[N];
	int cnt, i, i_, j, a, x, y, d;
	long long z;
	scanf("%d", &n);
	memset(ii, -1, n * sizeof *ii);
	for (i = 0; i < n; i++) {
		scanf("%d", &aa[i]), aa[i]--;
		if (aa[i] >= 0)
			ii[aa[i]] = i;
	}
	for (a = 0; a < n; a++)
		scanf("%d", &ww[a]);
	m = 0;
	for (a = 0; a < n; a++)
		if ((i = ii[a]) != -1)
			xx[i] = m;
		else
			ss[m + 1] = ss[m] + ww[a], m++;
	if (m == n) {
		for (i = 0; i < n; i++)
			printf("%lld ", ss[n] - ss[n - 1 - i]);
		printf("\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		pp[i] = qq[i] = -1, hh[i] = tt[i] = i;
	cnt = 0, y = 0, a = -1;
	for (j = 0; j < n; j++) {
		if (aa[j] == -2)
			y++;
		else if (a > aa[j])
			aa[j] = -1;
		else {
			a = aa[j];
			yy[j] = y, dd[j] = xx[j] - yy[j];
			if (dd[j] < 0)
				aa[j] = -1;
		}
		if (aa[j] >= 0) {
			i_ = -1;
			while (cnt && dd[i = qu[cnt - 1]] < dd[j]) {
				if (i_ == -1)
					i_ = i;
				else
					merge(i, i_);
				cnt--;
			}
			dp[j] = cnt ? dp[i] - ss[xx[i]] : -ss[dd[j]];
			if (i_ != -1)
				dp[j] = max(dp[j], query(i_, dd[j])), merge(i_, j);
			dp[j] += ss[xx[j]] + ww[aa[j]];
			qu[cnt++] = j;
		}
		x = m, d = x - y;
		z = 0;
		for (i = j; i >= 0; i--) {
			if (aa[i] < 0)
				continue;
			if (dd[i] >= d)
				break;
			z = max(z, dp[i] + ss[x] - ss[yy[i] + d]);
		}
		if (i >= 0)
			z = max(z, dp[i] + ss[x] - ss[xx[i]]);
		else
			z = max(z, ss[x] - ss[d]);
		printf("%lld ", z);
	}
	printf("\n");
	return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |   scanf("%d", &aa[i]), aa[i]--;
      |   ^~~~~~~~~~~~~~~~~~~
Main.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |   scanf("%d", &ww[a]);
      |   ^~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |