#include <bits/stdc++.h>
using namespace std;
const int maxd = 1e5 + 10;
long long dp[maxd], mx, ans[maxd], vct[maxd], arr[maxd], fa[maxd];
long long gf(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = gf(fa[x]);
}
int main() {
  int n;
  memset(dp, -1, sizeof(dp));
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) fa[i] = i;
  fa[0] = 1;
  for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  for (int i = 1; i <= n; i++) scanf("%lld", &vct[i]);
  for (int i = n; i >= 1; i--) {
    ans[i] = mx;
    int x = vct[i];
    dp[x] = arr[x];
    if (dp[x - 1] != -1) dp[x] += dp[gf(x - 1)];
    if (dp[x + 1] != -1) dp[x] += dp[gf(x + 1)];
    if (dp[x - 1] != -1) fa[gf(x - 1)] = x;
    if (dp[x + 1] != -1) fa[gf(x + 1)] = x;
    mx = max(dp[x], mx);
  }
  for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
  return 0;
}
